Home > database > ensemble_display_table.m

ensemble_display_table

PURPOSE ^

Mechanism for formatting display of data in a table.

SYNOPSIS ^

function data_st = ensemble_display_table(data_st,params)

DESCRIPTION ^

 Mechanism for formatting display of data in a table.

 data_st = ensemble_display_table(data_st,params)

 The data to be displayed should be stored in a cell array in variable called
 data.  
 Additional variables are:
   column_labels
   column_formats

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function data_st = ensemble_display_table(data_st,params)
0002 % Mechanism for formatting display of data in a table.
0003 %
0004 % data_st = ensemble_display_table(data_st,params)
0005 %
0006 % The data to be displayed should be stored in a cell array in variable called
0007 % data.
0008 % Additional variables are:
0009 %   column_labels
0010 %   column_formats
0011 
0012 % 05/01/2007 Petr Janata
0013 % 05/04/2007 PJ - fixed handling of cell arrays of strings that are passed
0014 %                 in as a "single" value
0015 % 07/24/2009 PJ - added optional replacement of NaNs
0016 
0017 % Check integrity of data struct
0018 
0019 % Set column indexing variable
0020 cols = set_var_col_const(data_st.vars);
0021 
0022 % Check display data to make sure we don't have any matrices within individual
0023 % columns
0024 ncols = size(data_st.data{cols.data},2);
0025 for icol = 1:ncols
0026   if numel(data_st.data{cols.data}{icol}) ~= ...
0027     length(data_st.data{cols.data}{icol})
0028     msgstr = sprintf('%s: Display data column contains a matrix', mfilename);
0029     error(msgstr);
0030   else
0031     data_st.data{cols.data}{icol} = data_st.data{cols.data}{icol}(:);
0032   end
0033 end
0034 
0035 % Get a file identifier to write the table to. Defaults to standard out.
0036 fid = ensemble_init_fid(params);
0037 
0038 % Print some header information
0039 try add_datestamp = params.add_datestamp; catch add_datestamp = false; end
0040 if add_datestamp
0041   fprintf(fid,'\nTable generated on: %s\n\n', datestr(now,0));
0042 end
0043 
0044 % Print the column names
0045 colnames = data_st.data{cols.column_labels};
0046 if length(colnames) ~= ncols
0047   msgstr = sprintf(['%s: Mismatch between number of data columns (%d) and number of ' ...
0048     'column labels (%d)', ncols, length(colnames)]);
0049   error(msgstr)
0050 end
0051 
0052 for icol = 1:ncols
0053   fprintf(fid,'%s', colnames{icol});
0054   if icol < ncols, fprintf(fid,'\t');
0055   else fprintf(fid,'\n');
0056   end
0057 end
0058 
0059 % See whether we are going to replace NaNs
0060 try replace_nan = params.replace_nan; catch replace_nan = false; end
0061 if replace_nan
0062   try nan_str = params.nan_str; catch nan_str = '.'; end
0063 end
0064 
0065 % Print rows
0066 nrows = length(data_st.data{cols.data}{1});
0067 for irow = 1:nrows
0068   for icol = 1:ncols
0069     format_str = data_st.data{cols.column_formats}{icol};
0070     if ~isempty(findstr(format_str,'s'))
0071       if iscell(data_st.data{cols.data}{icol}{irow})
0072         data_val = cell2str(data_st.data{cols.data}{icol}{irow},',');
0073       else
0074         data_val = data_st.data{cols.data}{icol}{irow};
0075       end
0076     else
0077       data_val = data_st.data{cols.data}{icol}(irow);
0078       if isnan(data_val) && replace_nan
0079         data_val = nan_str;
0080         format_str = '%s';
0081       end
0082     end
0083     fprintf(fid, format_str, data_val);
0084     if icol < ncols, fprintf(fid,'\t');
0085     else fprintf(fid,'\n');
0086     end
0087   end
0088 end
0089 
0090 if fid > 1
0091   fclose(fid);
0092 end

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003