text:
function tet_mesh_volumes ( prefix )

%*****************************************************************************80
%
%% MAIN is the main program for TET_MESH_VOLUMES.
%
%  Discussion:
%
%    TET_MESH_VOLUMES determines the element volumes of a tet mesh.
%
%  Usage:
%
%    tet_mesh_volumes ( 'prefix' )
%
%    where
%
%    * 'prefix'_nodes.txt contains nodal coordinates;
%    * 'prefix'_elements.txt contains the element definitions;
%    * 'prefix'_volumes.txt will contain the element volumes.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    12 August 2009
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  timestamp ( )

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TET_MESH_VOLUMES:\n' );
  fprintf ( 1, '  MATLAB version:\n' );
  fprintf ( 1, '  Compute volume of each tetrahedron in a tet mesh.\n' );
%
%  Argument 1 is the common file prefix.
%
  if ( 1 <= nargin )

  else

    fprintf ( 1, '\n' );
    fprintf ( 1, 'TET_MESH_VOLUMES:\n' );
    prefix = input ( '  Please enter the filename prefix:' );

  end
%
%  Create the filenames.
%
  node_filename = strcat ( prefix, '_nodes.txt' );
  element_filename = strcat ( prefix, '_elements.txt' );
  volume_filename = strcat ( prefix, '_volumes.txt' );
%
%  Read the node data.
%
  [ dim_num, node_num ] = r8mat_header_read ( node_filename );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Read the header of "%s".\n', node_filename );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Spatial dimension DIM_NUM = %d\n', dim_num );
  fprintf ( 1, '  Number of points NODE_NUM = %d\n', node_num );

  if ( dim_num ~= 3 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'TET_MESH_VOLUMES - Fatal error!\n' );
    fprintf ( 1, '  Dataset must have spatial dimension 3.\n' );
    error ( 'TET_MESH_VOLUMES - Fatal error!' );
  end

  node_xyz = r8mat_data_read ( node_filename, dim_num, node_num );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Read the data in "%s".\n', node_filename );

  r8mat_transpose_print_some ( dim_num, node_num, node_xyz, 1, 1, 5, 5, ...
    '  5 by 5 portion of data read from file:' );
%
%  Read the element data.
%
  [ element_order, element_num ] = i4mat_header_read ( element_filename );

  if ( element_order ~= 4 && element_order ~= 10 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'TET_MESH_VOLUMES - Fatal error!\n' );
    fprintf ( 1, '  Data is not for a 4 or 10 node tet mesh.\n' );
    error ( 'TET_MESH_VOLUMES - Fatal error!' );
  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Read the header of "%s".\n', element_filename );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Element order = %d\n', element_order );
  fprintf ( 1, '  Number of elements  = %d\n', element_num );

  element_node = i4mat_data_read ( element_filename, element_order, element_num );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Read the data in "%s".\n', element_filename );

  i4mat_transpose_print_some ( element_order, element_num, ...
    element_node, 1, 1, element_order, 10, '  Portion of TETRA_NODE:' );
%
%  Detect and correct 0-based indexing.
%
  element_node = mesh_base_one ( node_num, element_order, element_num, ...
    element_node );
%
%  Compute and the volumes.
%
  volume = zeros ( element_num );

  for element = 1 : element_num
    tetra(1:3,1:4) = node_xyz(1:3,element_node(1:4,element));
    volume(element) = tetrahedron_volume ( tetra );
  end

  volume_max = max ( volume(1:element_num) );
  volume_min = min ( volume(1:element_num) );
  volume_ave = sum ( volume(1:element_num) ) / element_num;
  volume_tot = sum ( volume(1:element_num) );
  volume_var = r8vec_variance ( element_num, volume );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Minimum:  %f\n', volume_min );
  fprintf ( 1, '  Average:  %f\n', volume_ave );
  fprintf ( 1, '  Maximum:  %f\n', volume_max );
  fprintf ( 1, '  Total:    %f\n', volume_tot );
  fprintf ( 1, '  Variance: %f\n', volume_var );

  r8mat_write ( volume_filename, 1, element_num, volume );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Full list of volumes written to "%s".\n',...
    volume_filename );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TET_MESH_VOLUMES:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  fprintf ( 1, '\n' );
  timestamp ( );

  return
end
function column_num = file_column_count ( input_file_name )

%*****************************************************************************80
%
%% FILE_COLUMN_COUNT counts the columns in the first line of a file.
%
%  Discussion:
%
%    The file is assumed to be a simple text file.
%
%    Most lines of the file are presumed to consist of COLUMN_NUM words,
%    separated by spaces.  There may also be some blank lines, and some
%    comment lines, which have a "#" in column 1.
%
%    The routine tries to find the first non-comment non-blank line and
%    counts the number of words in that line.
%
%    If all lines are blanks or comments, it goes back and tries to analyze
%    a comment line.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    21 February 2004
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, string INPUT_FILE_NAME, the name of the file.
%
%    Output, integer COLUMN_NUM, the number of columns in the file.
%
  FALSE = 0;
  TRUE = 1;
%
%  Open the file.
%
  input_unit = fopen ( input_file_name );

  if ( input_unit < 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'FILE_COLUMN_COUNT - Error!\n' );
    fprintf ( 1, '  Could not open the file "%s".\n', input_file_name );
    error ( 'FILE_COLUMN_COUNT - Error!' );
  end
%
%  Read one line, but skip blank lines and comment lines.
%  Use FGETL so we drop the newline character!
%
  got_one = FALSE;

  while ( 1 )

    line = fgetl ( input_unit );

    if ( line == -1 )
      break;
    end

    if ( s_len_trim ( line ) == 0 )

    elseif ( line(1) == '#' )

    else
      got_one = TRUE;
      break;
    end

  end

  fclose ( input_unit );

  if ( got_one == FALSE )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'FILE_COLUMN_COUNT - Warning!\n' );
    fprintf ( 1, '  The file does not seem to contain any data.\n' );
    column_num = -1;
    return;
  end

  column_num = s_word_count ( line );

  return
end
function row_num = file_row_count ( input_file_name )

%*****************************************************************************80
%
%% FILE_ROW_COUNT counts the number of row records in a file.
%
%  Discussion:
%
%    Each input line is a "RECORD".
%
%    The records are divided into three groups:
%
%    * BLANK LINES (nothing but blanks)
%    * COMMENT LINES (begin with a '#')
%    * DATA RECORDS (anything else)
%
%    The value returned by the function is the number of data records.
%
%    By the way, if the MATLAB routine FGETS is used, instead of
%    FGETL, then the variable LINE will include line termination
%    characters, which means that a blank line would not actually
%    have zero characters.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    31 December 2006
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, string INPUT_FILE_NAME, the name of the input file.
%
%    Output, integer ROW_NUM, the number of rows found.
%
  input_unit = fopen ( input_file_name );

  if ( input_unit < 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'FILE_ROW_COUNT - Error!\n' );
    fprintf ( 1, '  Could not open the file "%s".\n', input_file_name );
    error ( 'FILE_ROW_COUNT - Error!' );
  end

  blank_num = 0;
  comment_num = 0;
  row_num = 0;

  record_num = 0;

  while ( 1 )

    line = fgetl ( input_unit );

    if ( line == -1 )
      break;
    end

    record_num = record_num + 1;
    record_length = s_len_trim ( line );

    if ( record_length <= 0 )
      blank_num = blank_num + 1;
    elseif ( line(1) == '#' )
      comment_num = comment_num + 1;
    else
      row_num = row_num + 1;
    end

  end

  fclose ( input_unit );

  return
end
function table = i4mat_data_read ( input_filename, m, n )

%*****************************************************************************80
%
%% I4MAT_DATA_READ reads data from an I4MAT file.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    27 January 2006
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, string INPUT_FILENAME, the name of the input file.
%
%    Input, integer M, N, the number of rows and columns in the data.
%
%    Output, integer TABLE(M,N), the point coordinates.
%
  table = zeros ( m, n );
%
%  Build up the format string for reading M real numbers.
%
  string = ' ';

  for i = 0 : m
    string = strcat ( string, ' %d' );
  end

  input_unit = fopen ( input_filename );

  if ( input_unit < 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'I4MAT_DATA_READ - Error!\n' );
    fprintf ( 1, '  Could not open the input file.\n' );
    error ( 'I4MAT_DATA_READ - Error!' );
  end

  i = 0;

  while ( i < n )

    line = fgets ( input_unit );

    if ( line == -1 )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'I4MAT_DATA_READ - Error!\n' );
      fprintf ( 1, '  End of input while reading data.\n' );
      error ( 'I4MAT_DATA_READ - Error!' );
    end

    if ( line(1) == '#' )

    elseif ( s_len_trim ( line ) == 0 )

    else

      [ x, count ] = sscanf ( line, string );

      if ( count == m )
        i = i + 1;
        table(1:m,i) = x(1:m);
      end

    end

  end

  fclose ( input_unit );

  return
end
function [ m, n ] = i4mat_header_read ( input_filename )

%*****************************************************************************80
%
%% I4MAT_HEADER_READ reads the header from an I4MAT file.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    22 October 2004
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, string INPUT_FILENAME, the name of the input file.
%
%    Output, integer M, the spatial dimension.
%
%    Output, integer N, the number of points.
%
  m = file_column_count ( input_filename );

  if ( m <= 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'I4MAT_HEADER_READ - Fatal error!\n' );
    fprintf ( 1, '  There was some kind of I/O problem while trying\n' );
    fprintf ( 1, '  to count the number of data columns in\n' );
    fprintf ( 1, '  the file %s.\n', input_filename );
  end

  n = file_row_count ( input_filename );

  if ( n <= 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'I4MAT_HEADER_READ - Fatal error!\n' );
    fprintf ( 1, '  There was some kind of I/O problem while trying\n' );
    fprintf ( 1, '  to count the number of data rows in\n' );
    fprintf ( 1, '  the file %s\n', input_filename );
  end

  return
end
function i4mat_transpose_print_some ( m, n, a, ilo, jlo, ihi, jhi, title )

%*****************************************************************************80
%
%% I4MAT_TRANSPOSE_PRINT_SOME prints some of an I4MAT, transposed.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    21 June 2005
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer M, N, the number of rows and columns.
%
%    Input, integer A(M,N), an M by N matrix to be printed.
%
%    Input, integer ILO, JLO, the first row and column to print.
%
%    Input, integer IHI, JHI, the last row and column to print.
%
%    Input, string TITLE, an optional title.
%
  incx = 10;

  if ( 0 < s_len_trim ( title ) )
    fprintf ( 1, '\n' );
    fprintf ( 1, '%s\n', title );
  end

  for i2lo = max ( ilo, 1 ) : incx : min ( ihi, m )

    i2hi = i2lo + incx - 1;
    i2hi = min ( i2hi, m );
    i2hi = min ( i2hi, ihi );

    inc = i2hi + 1 - i2lo;

    fprintf ( 1, '\n' );
    fprintf ( 1, '  Row: ' );
    for i = i2lo : i2hi
      fprintf ( 1, '%7d  ', i );
    end
    fprintf ( 1, '\n' );
    fprintf ( 1, '  Col\n' );
    fprintf ( 1, '\n' );

    j2lo = max ( jlo, 1 );
    j2hi = min ( jhi, n );

    for j = j2lo : j2hi

      fprintf ( 1, '%5d  ', j );
      for i2 = 1 : inc
        i = i2lo - 1 + i2;
        fprintf ( 1, '%7d  ', a(i,j) );
      end
      fprintf ( 1, '\n' );

    end

  end

  return
end
function element_node = mesh_base_one ( node_num, element_order, ...
  element_num, element_node )

%*****************************************************************************80
%
%% MESH_BASE_ONE ensures that the element definition is one-based.
%
%  Discussion:
%
%    The ELEMENT_NODE array contains nodes indices that form elements.
%    The convention for node indexing might start at 0 or at 1.
%    Since a MATLAB program will naturally assume a 1-based indexing, it is
%    necessary to check a given element definition and, if it is actually
%    0-based, to convert it.
%
%    This function attempts to detect 0-based node indexing and correct it.
%
%    Thanks to Feifei Xu for pointing out that I was subtracting 1 when I
%    should have been adding 1!  29 November 2012.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    29 November 2012
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer NODE_NUM, the number of nodes.
%
%    Input, integer ELEMENT_ORDER, the order of the elements.
%
%    Input, integer ELEMENT_NUM, the number of elements.
%
%    Input/output, integer ELEMENT_NODE(ELEMENT_ORDE,ELEMENT_NUM), the element
%    definitions.
%
  node_min = min ( min ( element_node(1:element_order,1:element_num) ) );
  node_max = max ( max ( element_node(1:element_order,1:element_num) ) );

  if ( node_min == 0 && node_max == node_num - 1 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'MESH_BASE_ONE:\n' );
    fprintf ( 1, '  The element indexing appears to be 0-based!\n' );
    fprintf ( 1, '  This will be converted to 1-based.\n' );
    element_node(1:element_order,1:element_num) = ...
      element_node(1:element_order,1:element_num) + 1;
  elseif ( node_min == 1 && node_max == node_num )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'MESH_BASE_ONE:\n' );
    fprintf ( 1, '  The element indexing appears to be 1-based!\n' );
    fprintf ( 1, '  No conversion is necessary.\n' );
  else
    fprintf ( 1, '\n' );
    fprintf ( 1, 'MESH_BASE_ONE - Warning!\n' );
    fprintf ( 1, '  The element indexing is not of a recognized type.\n' );
  end

  return
end
function table = r8mat_data_read ( input_filename, m, n )

%*****************************************************************************80
%
%% R8MAT_DATA_READ reads data from an R8MAT file.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    27 January 2006
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, string INPUT_FILENAME, the name of the input file.
%
%    Input, integer M, N, the number of rows and columns of data.
%
%    Output, real TABLE(M,N), the point coordinates.
%
  table = zeros ( m, n );
%
%  Build up the format string for reading M real numbers.
%
  string = ' ';

  for i = 0 : m
    string = strcat ( string, ' %f' );
  end

  input_unit = fopen ( input_filename );

  if ( input_unit < 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'R8MAT_DATA_READ - Error!\n' );
    fprintf ( 1, '  Could not open the file.\n' );
    error ( 'R8MAT_DATA_READ - Error!' );
  end

  i = 0;

  while ( i < n )

    line = fgets ( input_unit );

    if ( line == -1 )
      break;
    end

    if ( line(1) == '#' )

    elseif ( s_len_trim ( line ) == 0 )

    else

      [ x, count ] = sscanf ( line, string );

      if ( count == m )
        i = i + 1;
        table(1:m,i) = x(1:m);
      end

    end

  end

  fclose ( input_unit );

  return
end
function det = r8mat_det_4d ( a )

%*****************************************************************************80
%
%% R8MAT_DET_4D computes the determinant of a 4 by 4 matrix.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    31 January 2005
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real A(4,4), the matrix whose determinant is desired.
%
%    Output, real DET, the determinant of the matrix.
%
  det = ...
      a(1,1) * ( ...
        a(2,2) * ( a(3,3) * a(4,4) - a(3,4) * a(4,3) ) ...
      - a(2,3) * ( a(3,2) * a(4,4) - a(3,4) * a(4,2) ) ...
      + a(2,4) * ( a(3,2) * a(4,3) - a(3,3) * a(4,2) ) ) ...
    - a(1,2) * ( ...
        a(2,1) * ( a(3,3) * a(4,4) - a(3,4) * a(4,3) ) ...
      - a(2,3) * ( a(3,1) * a(4,4) - a(3,4) * a(4,1) ) ...
      + a(2,4) * ( a(3,1) * a(4,3) - a(3,3) * a(4,1) ) ) ...
    + a(1,3) * ( ...
        a(2,1) * ( a(3,2) * a(4,4) - a(3,4) * a(4,2) ) ...
      - a(2,2) * ( a(3,1) * a(4,4) - a(3,4) * a(4,1) ) ...
      + a(2,4) * ( a(3,1) * a(4,2) - a(3,2) * a(4,1) ) ) ...
    - a(1,4) * ( ...
        a(2,1) * ( a(3,2) * a(4,3) - a(3,3) * a(4,2) ) ...
      - a(2,2) * ( a(3,1) * a(4,3) - a(3,3) * a(4,1) ) ...
      + a(2,3) * ( a(3,1) * a(4,2) - a(3,2) * a(4,1) ) );

  return
end
function [ m, n ] = r8mat_header_read ( input_filename )

%*****************************************************************************80
%
%% R8MAT_HEADER_READ reads the header from an R8MAT file.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    22 October 2004
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, string INPUT_FILENAME, the name of the input file.
%
%    Output, integer M, the spatial dimension.
%
%    Output, integer N, the number of points.
%
  m = file_column_count ( input_filename );

  if ( m <= 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'R8MAT_HEADER_READ - Fatal error!\n' );
    fprintf ( 1, '  There was some kind of I/O problem while trying\n' );
    fprintf ( 1, '  to count the number of data columns in\n' );
    fprintf ( 1, '  the file %s.\n', input_filename );
  end

  n = file_row_count ( input_filename );

  if ( n <= 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'R8MAT_HEADER_READ - Fatal error!\n' );
    fprintf ( 1, '  There was some kind of I/O problem while trying\n' );
    fprintf ( 1, '  to count the number of data rows in\n' );
    fprintf ( 1, '  the file %s\n', input_filename );
  end

  return
end
function r8mat_transpose_print_some ( m, n, a, ilo, jlo, ihi, jhi, title )

%*****************************************************************************80
%
%% R8MAT_TRANSPOSE_PRINT_SOME prints some of an R8MAT, transposed.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    23 May 2005
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer M, N, the number of rows and columns.
%
%    Input, real A(M,N), an M by N matrix to be printed.
%
%    Input, integer ILO, JLO, the first row and column to print.
%
%    Input, integer IHI, JHI, the last row and column to print.
%
%    Input, string TITLE, an optional title.
%
  incx = 5;

  if ( 0 < s_len_trim ( title ) )
    fprintf ( 1, '\n' );
    fprintf ( 1, '%s\n', title );
  end

  for i2lo = max ( ilo, 1 ) : incx : min ( ihi, m )

    i2hi = i2lo + incx - 1;
    i2hi = min ( i2hi, m );
    i2hi = min ( i2hi, ihi );

    inc = i2hi + 1 - i2lo;

    fprintf ( 1, '\n' );
    fprintf ( 1, '  Row: ' );
    for i = i2lo : i2hi
      fprintf ( 1, '%7d       ', i );
    end
    fprintf ( 1, '\n' );
    fprintf ( 1, '  Col\n' );

    j2lo = max ( jlo, 1 );
    j2hi = min ( jhi, n );

    for j = j2lo : j2hi

      fprintf ( 1, '%5d ', j );
      for i2 = 1 : inc
        i = i2lo - 1 + i2;
        fprintf ( 1, '%12f', a(i,j) );
      end
      fprintf ( 1, '\n' );

    end

  end

  return
end
function r8mat_write ( output_filename, m, n, table )

%*****************************************************************************80
%
%% R8MAT_WRITE writes an R8MAT file.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    09 August 2009
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, string OUTPUT_FILENAME, the output filename.
%
%    Input, integer M, the spatial dimension.
%
%    Input, integer N, the number of points.
%
%    Input, real TABLE(M,N), the points.
%

%
%  Open the file.
%
  output_unit = fopen ( output_filename, 'wt' );

  if ( output_unit < 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'R8MAT_WRITE - Error!\n' );
    fprintf ( 1, '  Could not open the output file.\n' );
    error ( 'R8MAT_WRITE - Error!' );
  end
%
%  Write the data.
%
%  For greater precision, try:
%
%     fprintf ( output_unit, '  %24,16f', table(i,j) );
%
  for j = 1 : n
    for i = 1 : m
      fprintf ( output_unit, '  %14f', table(i,j) );
    end
    fprintf ( output_unit, '\n' );
  end
%
%  Close the file.
%
  fclose ( output_unit );

  return
end
function mean = r8vec_mean ( n, x )

%*****************************************************************************80
%
%% R8VEC_MEAN returns the mean of an R8VEC.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    01 September 2004
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the number of entries in the vector.
%
%    Input, real X(N), the vector whose mean is desired.
%
%    Output, real MEAN, the mean, or average,
%    of the vector entries.
%
  mean = sum ( x(1:n) ) / n;

  return
end
function variance = r8vec_variance ( n, x )

%*****************************************************************************80
%
%% R8VEC_VARIANCE returns the variance of an R8VEC.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    01 September 2004
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the number of entries in the vector.
%
%    Input, real X(N), the vector whose variance is desired.
%
%    Output, real VARIANCE, the variance of the vector entries.
%
  mean = r8vec_mean ( n, x );

  variance = sum ( ( x(1:n) - mean ).^2 );

  if ( 1 < n )
    variance = variance / ( n - 1 );
  else
    variance = 0.0;
  end

  return
end
function len = s_len_trim ( s )

%*****************************************************************************80
%
% S_LEN_TRIM returns the length of a character string to the last nonblank.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    14 June 2003
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, string S, the string to be measured.
%
%    Output, integer LENGTH, the length of the string up to the last nonblank.
%
  len = length ( s );

  while ( 0 < len )
    if ( s(len) ~= ' ' )
      return
    end
    len = len - 1;
  end

  return
end
function word_num = s_word_count ( s )

%*****************************************************************************80
%
%% S_WORD_COUNT counts the number of "words" in a string.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    30 January 2006
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, string S, the string to be examined.
%
%    Output, integer WORD_NUM, the number of "words" in the string.
%    Words are presumed to be separated by one or more blanks.
%
  FALSE = 0;
  TRUE = 1;

  word_num = 0;
  s_length = length ( s );

  if ( s_length <= 0 )
    return;
  end

  blank = TRUE;

  for i = 1 : s_length

    if ( s(i) == ' ' )
      blank = TRUE;
    elseif ( blank == TRUE )
      word_num = word_num + 1;
      blank = FALSE;
    end

  end

  return
end
function volume = tetrahedron_volume ( tetra )

%*****************************************************************************80
%
%% TETRAHEDRON_VOLUME computes the volume of a tetrahedron.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    22 May 2005
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real TETRA(3,4), the vertices of the tetrahedron.
%
%    Output, real VOLUME, the volume of the tetrahedron.
%
  dim_num = 3;

  a(1:dim_num,1:4) = tetra(1:dim_num,1:4);
  a(4,1:4) = 1.0;

  volume = abs ( r8mat_det_4d ( a ) ) / 6.0;

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% TIMESTAMP prints the current YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  t = now;
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end


text:
% Fig. 9.43   Feedback Control of Dynamic Systems, 5e
%             Franklin, Powell, Emami
%

%script to plot the phase plane for a bang-bang example
figure(1)
hold off
clf
N=-1;
x = -100;
xdot=  sqrt(200);
sim('bang')
plot(xbang(:,2),xdotbang(:,2));
hold on
N=1;
x = 100;
xdot=-sqrt(200);
sim('bang');
plot(xbang(:,2),xdotbang(:,2));
xlabel('x_1');
ylabel('x_2');
hold on
title(' Switching curve for 1/s^2 plant')
grid on
%script to plot the phase plane for a bang-bang example
figure(1)
hold off
clf
N=-1;
x = -100;
xdot=  sqrt(200);
sim('bang')
plot(xbang(:,2),xdotbang(:,2));
hold on
N=1;
x = 100;
xdot=-sqrt(200);
sim('bang');
plot(xbang(:,2),xdotbang(:,2));
xlabel('x_1');
ylabel('x_2');
hold on
title('Switching curve for 1/s^2 plant');
grid on

author: Sable

repo: mcbench-benchmarks


text:
%TRAINCC Train combining classifier if needed
%
%   W = TRAINCC(A,W,CCLASSF)
%
% INPUT
%   A        Training dataset
%   W        A set of classifiers to be combined
%   CCLASSF  Combining classifier
%
% OUTPUT
%   B        Combined classifier mapping
%
% DESCRIPTION
% The combining classifier CCLASSF is trained by the dataset A*W, if
% training is needed. W is typically a set of stacked (operating in the same
% feature space) or parallel (operating in different feature spaces;
% performed one after another) classifiers to be combined. E.g. if V1, V2
% and V3 are base classifiers, then V = [V1,V2,V3,...] is a stacked
% classifier and V = [V1;V2;V3;...] is a parallel one. If CCLASSF is one of
% the fixed combining rules like MAXC, then training is skipped.
%
% This routine is typically called by combining classifier schemes like
% BAGGINGC and BOOSTINGC.
%
% SEE ALSO (PRTools Guide)
% DATASETS, MAPPINGS, STACKED, PARALLEL, BAGGINGC

% Copyright: R.P.W. Duin, duin@ph.tn.tudelft.nl
% Faculty of Applied Sciences, Delft University of Technology
% P.O. Box 5046, 2600 GA Delft, The Netherlands

% $Id: traincc.m,v 1.4 2010/06/25 07:55:34 duin Exp $

function w = traincc(a,w,cclassf)

		if (~ismapping(cclassf))
		error('Combining classifier is an unknown mapping.')
	end

	% If CCLASSF is already a combining classifier, just apply it. Otherwise,
	% train it using A*W.
	if isuntrained(w)
		w = a*w;  % train base classifiers
	end

	w = w*cclassf;
	if isuntrained(w)
		w = a*w;  % train combiner when needed
	end

	w = setcost(w,a);

return

author: marianux

repo: ecg-kit

save_path: github-repos/MATLAB/marianux-ecg-kit


text:
function b = p04_b ( m )

%*****************************************************************************80
%
%% P04_B returns the right hand side B for problem 4.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    16 April 2012
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer M, the number of equations.
%
%    Output, real B(M,1), the right hand side.
%
  b = [ 15.0; 55.0; 225.0 ];

  return
end

author: johannesgerer

repo: jburkardt-m


text:
function varargout = bdfVisualizer(varargin)
% BDFVISUALIZER MATLAB code for bdfVisualizer.fig
%      BDFVISUALIZER, by itself, creates a new BDFVISUALIZER or raises the existing
%      singleton*.
%
%      H = BDFVISUALIZER returns the handle to a new BDFVISUALIZER or the handle to
%      the existing singleton*.
%
%      BDFVISUALIZER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in BDFVISUALIZER.M with the given input arguments.
%
%      BDFVISUALIZER('Property','Value',...) creates a new BDFVISUALIZER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before bdfVisualizer_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to bdfVisualizer_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help bdfVisualizer

% Last Modified by GUIDE v2.5 15-Jan-2015 15:52:48

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @bdfVisualizer_OpeningFcn, ...
    'gui_OutputFcn',  @bdfVisualizer_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before bdfVisualizer is made visible.
function bdfVisualizer_OpeningFcn(hObject, ~, handles, varargin) %#ok<*DEFNU>
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to bdfVisualizer (see VARARGIN)

if(verLessThan('matlab', '8.2'))
    err_msg   = sprintf('Upgrade to Matlab 8.2 (2013b) or higher.\n\n BDF Visualizer will not run correctly for this Matlab version: %s.', version);
    err_title = 'Matlab Version Incompatibility';
    errorfound(err_msg, err_title);
    return;
end

% Choose default command line output for bdfVisualizer
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes bdfVisualizer wait for user response (see UIRESUME)
% uiwait(handles.windowBDFVisualizer);

%% Load default ELIST
handles.EEG                  = struct();
handles.EEG.EVENTLIST        = generateDefaultEventlistStruct();                      % nested function within BDFVISUALIZER.M
guidata(hObject, handles);                                                            % save EVENTLIST to HANDLES

% UPDATE GUI: ELIST Window
% tableEventList                    = struct2table(handles.EEG.EVENTLIST.eventinfo);
% handles.numBits                   = 16;
% tableEventList.bini               = [];
%
% % Convert the flag variable into its binary representation and split into
% % its user-flag and artifact-flag
% binaryFlag                        = dec2bin(tableEventList.flag, handles.numBits);    % Convert artifact flags & user flags to binary
% tableEventList.artifactFlag       = binaryFlag(:, 1:handles.numBits/2);
% tableEventList.userFlag           = binaryFlag(:, handles.numBits/2+1:end);
%
%
% % Rearrange the event list table so that the bin label is a the end
% tableEventList                    = tableEventList(:,[1:9 11:12 10]);

% Save the event-list table to the UI-table
tableEventList                    = eventlist2table(handles.EEG.EVENTLIST.eventinfo);
hUITableELIST                     = handle(handles.uitableELIST);
hUITableELIST.Data                = table2cell(tableEventList);
hUITableELIST.ColumnName          = tableEventList.Properties.VariableNames';

% UPDATE GUI: Eventlist Max window
hEditEventlistMax                 = handle(handles.editTxtEventlistMax);              % Retrieve data from the GUI
hEditEventlistMax.String          = num2str(length(hUITableELIST.Data));

%% Load default BDF
handles.bdf                     = generateDefaultBDFStruct();                         % load the text-string into HANDLES

hEditBDF                        = handle(handles.editBDF);
hEditBDF.String                 = handles.bdf;                                        % Display BDF to GUI

%% Initiate default BDF Feedback Window
hUITableBinlisterFeedback         = handle(handles.uitableBinlisterFeedback);
hUITableBinlisterFeedback.Data    = {'Total Event Codes', 'Bin 1'; 0         , 0        };

handles.lastPath = pwd;
% handles.EEG      = [];
% Update handles structure
guidata(hObject, handles);                                                          % save HANDLES structure to GUI

% --- Outputs from this function are returned to the command line.
function varargout = bdfVisualizer_OutputFcn(~, ~, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

function editBDF_Callback(~, ~, ~)
% hObject    handle to editBDF (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editBDF as text
%        str2double(get(hObject,'String')) returns contents of editBDF as a double

% --- Executes during object creation, after setting all properties.
function editBDF_CreateFcn(hObject, ~, ~)
% hObject    handle to editBDF (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbuttonAnalyzeBDF.
function pushbuttonAnalyzeBDF_Callback(~, ~, handles)
% hObject    handle to pushbuttonAnalyzeBDF (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

try
    % Turn the interface off for processing.
    InterfaceObj=findobj(handle(handles.windowBDFVisualizer),'Enable','on');
    set(InterfaceObj,'Enable','off');
    drawnow;

    hEditEventlistMax   = handle(handles.editTxtEventlistMax);              % Retrieve BDF data from the GUI
    eventlistMax        = str2double(hEditEventlistMax.String);

    % Load BDF
    hEditBDF        = handle(handles.editBDF);              % Retrieve BDF data from the GUI
    BDFfilename     = fullfile(pwd,'BDF-tmp.txt');          % Create temporary BDF-file
    fileID          = fopen(BDFfilename, 'wt');             %
    fprintf(fileID,'%s\n', hEditBDF.String{:});             %
    fclose( fileID);

    % Load ELIST
    objELIST                    = handle(handles.uitableELIST);                         % Get the current Event List from the GUI

    handles.EEG.EVENTLIST.eventinfo = cell2struct(objELIST.Data(1:eventlistMax,:), objELIST.ColumnName', 2);  %

    %% RUN BINLISTER
    [handles.EEG, handles.EEG.EVENTLIST]  = binlister( handles.EEG ... % emptyEEG
        , BDFfilename       ...         % inputBinDescriptorFile
        , 'none'            ...         % inputEventList
        , 'none'            ...         % outputEventList
        , []                ...         % forbiddenCodeArray
        , []                ...         % ignoreCodeArray
        , 0                 );          % reportable

    if(isempty(handles.EEG.EVENTLIST));
        % Turn the interface back on
        set(InterfaceObj,'Enable','on');
        drawnow;
        return;
    end

    %% Display updated ELIST-struct to GUI
    %     tableEventList                    = struct2table(handles.EEG.EVENTLIST.eventinfo);
    %     tableEventList.binaryFlag         = dec2bin(tableEventList.flag, handles.numBits);                % Convert artifact flags & user flags to binary
    %
    % convert each numeric column to a string in order to save to
    % UITABLE.DATA
    %     for col_index = 1:width(tableEventList)
    %         if(isnumeric(tableEventList.(col_index)))
    %             tableEventList.(col_index) = num2str(tableEventList.(col_index));
    %         end
    %     end

    %     for col_index = 1:width(tableEventList)
    %         if(isnumeric(tableEventList.(col_index)))
    %             columnSize = size(tableEventList.(col_index), 2);
    %             if(columnSize > 1)
    %                 tableEventList.(col_index) = num2str(tableEventList.(col_index));
    %             end
    %         end
    %     end

    %     tableEventList.bini = [];

    tableEventList                    = eventlist2table(handles.EEG.EVENTLIST.eventinfo);
    hUITableELIST                     = handle(handles.uitableELIST);
    hUITableELIST.Data                = table2cell(tableEventList);
    hUITableELIST.ColumnName          = tableEventList.Properties.VariableNames';

    drawnow;

    % hUITableELIST.columnWidth         = 'auto';
    % hUITableELIST.columnName          = fieldnames(handles.EEG.EVENTLIST.eventinfo);

    %% Display updated BDF Feedback window
    totalEvents                       = length(handles.EEG.EVENTLIST.eventinfo);
    hUITableBinlisterFeedback         = handle(handles.uitableBinlisterFeedback);
    %     hUITableBinlisterFeedback.rowName = { 'Total Event Codes', handles.EEG.EVENTLIST.bdf.namebin };
    hUITableBinlisterFeedback.Data    = [{'Total Event Codes' handles.EEG.EVENTLIST.bdf.namebin}', num2cell([totalEvents handles.EEG.EVENTLIST.trialsperbin])'];
    %% Cleanup
    delete(BDFfilename);                                    % Delete temporary BDF-file
    %     delete(ELISTfilename);                                  % Delete temporary ELIST-file
    %     set( findall(handles.windowBDFVisualizer, '-property', 'Enable'), 'Enable', 'on')

    % Turn the interface back on
    set(InterfaceObj,'Enable','on');
    drawnow;

catch errorObj
    % If there is a problem, display the error message
    display(getReport(errorObj,'extended','hyperlinks','on'),'Error');
    %     set(InterfaceObj,'Enable','on');

    if(strcmpi(errorObj.stack(1).name, 'binlister') && errorObj.stack(1).line == 706)
        errordlg(sprintf('\n\n\tCannot analyze a BDF file containing RT-flags without an EEG dataset.\n\n\tRemove exist RT-flags from the BDF-file or load an existing EEG dataset.\n\n'), 'RT-Flag Error');
    elseif(strcmpi(errorObj.stack(1).name, 'pushbuttonAnalyzeBDF_Callback') && errorObj.stack(1).line == 171)
        errordlg(sprintf('\n\n\tBin numbers must be in sequential order.\n\n\tFix your bin numbers.\n\n'), 'BDF - Bin Number Error');
    else
        errordlg(getReport(errorObj,'extended','hyperlinks','off'),'Error');
    end

    % Turn the interface back on
    set(InterfaceObj,'Enable','on');
    drawnow;

end

% --- Executes on button press in pushbuttonLoadBDF.
function pushbuttonLoadBDF_Callback(hObject, ~, handles)
% hObject    handle to pushbuttonLoadBDF (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

try

    [fileName, pathName, filterIndex] = uigetfile(              ...
        {'*.txt', 'Select a bin descriptor file (BDF)'   ...
        ; '*.*'  , 'All files (*.*)'                   } ...
        , 'Select BDF File' ...
        , handles.lastPath);
    if(pathName)
        handles.lastPath = pathName;                                             % Update the last directory in HANDLES
        guidata(hObject,handles);                                                % Update the HANDLES data-structure
    end

    switch(filterIndex)
        case 0
            % User-selected Cancel
            display('User selected cancel');
        otherwise
            % Load file into ELIST-structure via READEVENTLIST
            BDFfilename                 = fullfile(pathName,fileName);
            fileID                      = fopen(BDFfilename);
            fileString                  = textscan(fileID, '%s', 'Delimiter','\n');
            fclose(fileID);

            handles.bdf                 = fileString{1};    % Update the BDF variable in HANDLES
            handles.lastPath            = pathName;         % Update the last directory in HANDLES
            guidata(hObject,handles);                       % Update the HANDLES data-structure

            %% DISPLAY LOADED BDF TO GUI
            hEditBDF               = handle(handles.editBDF);
            hEditBDF.String        = handles.bdf;
    end

catch errorObj
    % If there is a problem, display the error message
    display(getReport(errorObj,'extended','hyperlinks','on'),'Error');
    %     set(InterfaceObj,'Enable','on');

    if(strcmpi(errorObj.stack(1).name, 'readeventlist') && errorObj.stack(1).line == 140)
        errordlg(sprintf('\n\nIncorrect File Type:\tFile is not an acceptable BIN DESCRIPTOR FILE.\n\nSelect another BDF0file\n\n'), 'Incorrect File Type Error');
    else
        errordlg(getReport(errorObj,'extended','hyperlinks','off'),'Error');
    end

end

% --- Executes on button press in pushbuttonImportEventList.
function pushbuttonImportEventList_Callback(hObject, ~, handles)
% hObject    handle to pushbuttonImportEventList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

try
    % Turn the interface off for processing.
    InterfaceObj=findobj(handle(handles.windowBDFVisualizer),'Enable','on');
    set(InterfaceObj,'Enable','off');
    drawnow;

    [fileName, pathName]        = uigetfile(               ...
        {'*.txt;*.set', 'ELIST (*.txt) or EEG file (*.set)';            ...
        '*.*','All Files (*.*)'                                }        ...
        , 'MultiSelect', 'off'                                          ...
        , 'Select Event List Source'                                    ...
        , handles.lastPath);   % Get filename/filepath

    if(pathName)
        % Clear the Binlister Feedback uiTable
        hUITableBinlisterFeedback         = handle(handles.uitableBinlisterFeedback);
        hUITableBinlisterFeedback.Data    = {'Total Event Codes', 'Bin 1'; 0         , 0         };

        [~,~,fileExtension] = fileparts(fileName);
        handles.lastPath    = pathName;                                                             % Update the last directory in HANDLES
        guidata(hObject,handles);                                                                   % Update the HANDLES data-structure
    else
        fileExtension       = 'cancel';
    end

    % Check if FILE SELECTED or if CANCEL is selected

    switch(fileExtension)
        case 'cancel'  % CANCEL
            display('User selected cancel');                                                    % User-selected Cancel
        case '.txt'  % ELIST-TXT file

            ELISTfilename               = fullfile(pathName,fileName);                          % Load file into ELIST-structure via READEVENTLIST
            [~, handles.EEG.EVENTLIST]  = readeventlist([], ELISTfilename);

            %% !!!!!!!!!!!!!!!!!!!!!!!!!!!
            % Display updated ELIST-struct to GUI
            tableEventList                    = eventlist2table(handles.EEG.EVENTLIST.eventinfo);
            hUITableELIST                     = handle(handles.uitableELIST);
            hUITableELIST.Data                = table2cell(tableEventList);
            hUITableELIST.ColumnName          = tableEventList.Properties.VariableNames';

            % UPDATE GUI: Eventlist Max window
            hEditEventlistMax                 = handle(handles.editTxtEventlistMax);              % Retrieve data from the GUI
            hEditEventlistMax.String          = num2str(length(hUITableELIST.Data));

        case '.set' % EEG-SET file
            handles.EEG = pop_loadset('filename',fileName,'filepath',pathName);
            handles.EEG = eeg_checkset( handles.EEG );

            % If EEG.EVENTLIST does not exist
            if(~isfield(handles.EEG, 'EVENTLIST'))
                % Create EVENTLIST from EEG dataset
                handles.EEG  = pop_creabasiceventlist( handles.EEG ...
                    ... %         , 'Eventlist','/Users/etfoo/Documents/MATLAB/Eventlist.txt' ...
                    , 'AlphanumericCleaning'    , 'on'              ...
                    , 'BoundaryNumeric'         , { -99 }           ...
                    , 'BoundaryString'          , { 'boundary' }    ...
                    , 'Warning'                 , 'off'              ...
                    );
            end

            %% !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            % Update the GUI ELIST uiTable
            tableEventList                    = eventlist2table(handles.EEG.EVENTLIST.eventinfo);
            hUITableELIST                     = handle(handles.uitableELIST);
            hUITableELIST.Data                = table2cell(tableEventList);
            hUITableELIST.ColumnName          = tableEventList.Properties.VariableNames';

            % UPDATE GUI: Eventlist Max window
            hEditEventlistMax                 = handle(handles.editTxtEventlistMax);              % Retrieve data from the GUI
            hEditEventlistMax.String          = num2str(length(hUITableELIST.Data));

        otherwise
    end

    guidata(hObject,handles);                                                                   % Update the HANDLES data-structure

    % Turn the interface back on
    set(InterfaceObj,'Enable','on');
    drawnow;

catch errorObj
    % If there is a problem, display the error message
    display(getReport(errorObj,'extended','hyperlinks','on'),'Error');
    %     set(InterfaceObj,'Enable','on');

    if(strcmpi(errorObj.stack(1).name, 'readeventlist') && errorObj.stack(1).line == 140)
        errordlg(sprintf('\n\nIncorrect File Type:\tFile does not contain an acceptable EVENT LIST FILE.\n\nSelect another file\n\n'), 'Incorrect File Type Error');
    else
        errordlg(getReport(errorObj,'extended','hyperlinks','off'),'Error');
    end

    % Turn the interface back on
    set(InterfaceObj,'Enable','on');
    drawnow;

end

function editTxtEventlistMax_Callback(~, ~, ~)
% hObject    handle to editTxtEventlistMax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editTxtEventlistMax as text
%        str2double(get(hObject,'String')) returns contents of editTxtEventlistMax as a double

% --- Executes during object creation, after setting all properties.
function editTxtEventlistMax_CreateFcn(hObject, ~, ~)
% hObject    handle to editTxtEventlistMax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbuttonBDFManualLink.
function pushbuttonBDFManualLink_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonBDFManualLink (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
web('https://github.com/lucklab/erplab/wiki/Assigning-Events-to-Bins-with-BINLISTER','-browser');

% --- Executes on button press in pushbuttonBDFTutorialLink.
function pushbuttonBDFTutorialLink_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonBDFTutorialLink (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
web('https://github.com/lucklab/erplab/wiki/BDF-Library','-browser');

%% Nested functions

function eventlist = generateDefaultEventlistStruct()

eventlist                   = struct;
eventlist.setname           = 'none_specified';
eventlist.report            = '';
eventlist.bdfname           = '';
eventlist.nbin              = 0;
eventlist.version           = '4.0.3.1';
eventlist.account           = '';
eventlist.username          = '';
eventlist.trialsperbin      = 0;
eventlist.elname            = 'default eventlist';
eventlist.bdf               = struct;
eventlist.bdf.expression    = [];
eventlist.bdf.description   = [];
eventlist.bdf.prehome       = [];
eventlist.bdf.athome        = [];
eventlist.bdf.posthome      = [];
eventlist.bdf.namebin       = [];
eventlist.bdf.rtname        = [];
eventlist.bdf.rtindex       = [];
eventlist.bdf.rt            = [];
eventlist.eldate            = '14-Jan-2015 15:24:22';
eventlist.eventinfo                 = struct;

for x = 1:100
    eventlist.eventinfo(x).item         = 1;
    eventlist.eventinfo(x).bepoch       = 0;
    eventlist.eventinfo(x).code         = 256;
    eventlist.eventinfo(x).codelabel    = '"EMPTY"';
    eventlist.eventinfo(x).time         = single(x/10);
    eventlist.eventinfo(x).spoint       = single(x);
    eventlist.eventinfo(x).dura         = single(x);
    eventlist.eventinfo(x).flag         = 0;
    eventlist.eventinfo(x).enable       = 1;
    eventlist.eventinfo(x).bini         = -1;
    eventlist.eventinfo(x).binlabel     = '""';
end

function bdfFile = generateDefaultBDFStruct()

bdfFile      = cell(3, 1);
bdfFile{1,1} = 'bin 1';
bdfFile{2,1} = 'Empty trials';
bdfFile{3,1} = '.{256}';

function eventlistTable = eventlist2table(eventlistStruct)

% UPDATE GUI: ELIST Window
eventlistTable                    = struct2table(eventlistStruct);
handles.numBits                   = 16;
eventlistTable.bini               = [];

% Convert the flag variable into its binary representation and split into
% its user-flag and artifact-flag
mybinaryFlag                      = dec2bin(eventlistTable.flag, handles.numBits);    % Convert artifact flags & user flags to binary
eventlistTable.artifactFlag       = mybinaryFlag(:, handles.numBits/2+1:end);
eventlistTable.userFlag           = mybinaryFlag(:, 1:handles.numBits/2);

% Rearrange the event list table so that the bin label is a the end
varnames = eventlistTable.Properties.VariableNames;
others = ~strcmp('binlabel',varnames);
varnames = [varnames(others) 'binlabel'];
eventlistTable = eventlistTable(:,varnames);

author: ucdavis

repo: erplab

save_path: github-repos/MATLAB/ucdavis-erplab