Home > mrg > MRG_utilities > mrg_struct_to_csv.m

mrg_struct_to_csv

PURPOSE ^

A function to output a structure to csv. Currently can only deal with a

SYNOPSIS ^

function mrg_struct_to_csv(in, outfile)

DESCRIPTION ^

 A function to output a structure to csv.  Currently can only deal with a
 limited number of data types, and only deals with 'square' structures.

 INPUT
   in          A structure with equal-length fields of either cell arrays
               or things that test true for is isnumeric(). Other things
               will fail.  
   outfile     A character string giving the (possibly fully qualified)
               output filename.
 
 LICENCE
   Created by Daniel Pritchard (www.pritchard.co)
   Distributed under a creative commons CC BY-SA licence. See here:
   http://creativecommons.org/licenses/by-sa/3.0/

 DEVELOPMENT
   v 1.0   Time Immemorial
           DP.  Why does MATLAB not do this by defualt?
   v 1.1   November 2012
           DP.  Documentation.   
   v 1.2   DP. Silently strips ',' from strings
   v 1.3   DP. Quoting string is nicer, lets do that.

 TODO
   Catch input.  Prompt user for filename if not present.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function mrg_struct_to_csv(in, outfile)
0002 % A function to output a structure to csv.  Currently can only deal with a
0003 % limited number of data types, and only deals with 'square' structures.
0004 %
0005 % INPUT
0006 %   in          A structure with equal-length fields of either cell arrays
0007 %               or things that test true for is isnumeric(). Other things
0008 %               will fail.
0009 %   outfile     A character string giving the (possibly fully qualified)
0010 %               output filename.
0011 %
0012 % LICENCE
0013 %   Created by Daniel Pritchard (www.pritchard.co)
0014 %   Distributed under a creative commons CC BY-SA licence. See here:
0015 %   http://creativecommons.org/licenses/by-sa/3.0/
0016 %
0017 % DEVELOPMENT
0018 %   v 1.0   Time Immemorial
0019 %           DP.  Why does MATLAB not do this by defualt?
0020 %   v 1.1   November 2012
0021 %           DP.  Documentation.
0022 %   v 1.2   DP. Silently strips ',' from strings
0023 %   v 1.3   DP. Quoting string is nicer, lets do that.
0024 %
0025 % TODO
0026 %   Catch input.  Prompt user for filename if not present.
0027 
0028 %% Go
0029 if ~isstruct(in)
0030     error('Input must be a structure')
0031 end
0032 
0033 if ~ischar(outfile)
0034     error('Outfile must be a charater string')
0035 end
0036 
0037 names = fieldnames(in);
0038 ncol = length(names);
0039 
0040 lengths = ones(1,length(names),'int32');
0041 for n = 1:length(names)
0042     lengths(1,n) = length(in.(names{n}));
0043 end
0044 
0045 if~(all(lengths==lengths(1)))
0046     error('All variables in the input structure must have the same length')
0047 end
0048 
0049 nrow = unique(lengths);
0050 
0051 fid = fopen(outfile, 'w');
0052 
0053 for n = 1:length(names)-1
0054     fprintf(fid, '%s,', names{n});
0055 end
0056 fprintf(fid, '%s\n', names{length(names)});
0057 
0058 for n = 1:nrow
0059     % Do something for each of the columns
0060     for m = 1:length(names)-1
0061         val = in.(names{m})(n);
0062         if iscell(val)
0063             if isnumeric(val{:})
0064                 fprintf(fid, '%f,', val{:});
0065             elseif ischar(val{:})
0066                 %fprintf(fid, '%s,', strrep(val{:}, ',', ''));
0067                 fprintf(fid, '"%s",', val{:});
0068             else
0069                 error('Cannot handle a cell array which does not evaluate to either a number or a character')
0070             end
0071         elseif isnumeric(val)
0072             fprintf(fid, '%f,', val(:));
0073         else
0074             error('Cannot handle something that contains things other than cell or numeric arrays')
0075         end
0076     end
0077     % Do something different for the last column
0078     val = in.(names{length(names)})(n);
0079     if iscell(val)
0080         if isnumeric(val{:})
0081             fprintf(fid, '%f\n', val{:});
0082         elseif ischar(val{:})
0083             fprintf(fid, '%s\n', val{:});
0084         else
0085             error('Cannot handle a cell array which does not evaluate to either a number or a character')
0086         end
0087     elseif isnumeric(val)
0088         fprintf(fid, '%f\n', val(:));
0089     else
0090         error('Cannot handle something that contains things other than cell or numeric arrays')
0091     end
0092     
0093     
0094 end
0095 
0096 fclose(fid);
0097 
0098 end

Generated on Thu 29-May-2014 21:29:53 by m2html © 2005