Home > m > helper > cell2str.m

cell2str

PURPOSE ^

CELL2STR Convert a 2-D cell array to a string in MATLAB syntax.

SYNOPSIS ^

function string = cell2str(cellstr)

DESCRIPTION ^

CELL2STR Convert a 2-D cell array to a string in MATLAB syntax.
   STR = CELL2STR(CELLSTR) converts the 2-D CELLSTR to a 
   MATLAB string so that EVAL(STR) produces the original cell-array.
   Works as corresponding MAT2STR but for cell array instead of 
   scalar matrices.

   Example
       cellstr = {'U-234','Th-230'};
       cell2str(cellstr) produces the string '{''U-234'',''Th-230'';}'.

   See also MAT2STR, STRREP, CELLFUN, EVAL.
   Developed by Per-Anders Ekström, 2003-2007 Facilia AB.

   Modified by Nicola Tateo for the LTPDA toolbox, to work also with cell
   arrays of numbers and to remove the last ';'

 $Id: cell2str.m,v 1.4 2008/03/01 15:16:15 hewitson Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function string = cell2str(cellstr)
0002 %CELL2STR Convert a 2-D cell array to a string in MATLAB syntax.
0003 %   STR = CELL2STR(CELLSTR) converts the 2-D CELLSTR to a
0004 %   MATLAB string so that EVAL(STR) produces the original cell-array.
0005 %   Works as corresponding MAT2STR but for cell array instead of
0006 %   scalar matrices.
0007 %
0008 %   Example
0009 %       cellstr = {'U-234','Th-230'};
0010 %       cell2str(cellstr) produces the string '{''U-234'',''Th-230'';}'.
0011 %
0012 %   See also MAT2STR, STRREP, CELLFUN, EVAL.
0013 %   Developed by Per-Anders Ekström, 2003-2007 Facilia AB.
0014 %
0015 %   Modified by Nicola Tateo for the LTPDA toolbox, to work also with cell
0016 %   arrays of numbers and to remove the last ';'
0017 %
0018 % $Id: cell2str.m,v 1.4 2008/03/01 15:16:15 hewitson Exp $
0019 %
0020 
0021 if nargin~=1
0022     error('CELL2STR:Nargin','Takes 1 input argument.');
0023 end
0024 if ischar(cellstr)
0025    string = ['''' strrep(cellstr,'''','''''') ''''];
0026    return
0027 end
0028 if ndims(cellstr)>2
0029     error('CELL2STR:TwoDInput','Input cell array must be 2-D.');
0030 end
0031 
0032 if iscellstr(cellstr)
0033     ncols = size(cellstr,2);
0034     for i=1:ncols-1
0035         output(:,i) = cellfun(@(x)['''' strrep(x,'''','''''') ''','],...
0036             cellstr(:,i),'UniformOutput',false);
0037     end
0038     if ncols>0
0039         output(:,ncols) = cellfun(@(x)['''' strrep(x,'''','''''') ''';'],...
0040             cellstr(:,ncols),'UniformOutput',false);
0041     end
0042     output = output';
0043     output{numel(output)}(numel(output{numel(output)})) = [];
0044     string = ['{' output{:} '}'];
0045 else
0046     output = mat2str(cell2mat(cellstr));
0047     if numel(output)>1, string = ['{',output(2:numel(output)-1),'}'];
0048     else string = ['{',output,'}'];
0049     end
0050 end
0051 
0052

Generated on Mon 31-Mar-2008 13:54:54 by m2html © 2003