Home > source > cell2str.m

cell2str

PURPOSE ^

Converts a 2-D cell array of strings to a string in MATLAB syntax.

SYNOPSIS ^

function string = cell2str( cellstr )

DESCRIPTION ^

Converts a 2-D cell array of strings to a string in MATLAB syntax.

STR = CELL2STR(CELLSTR) converts the 2-D cell-string CELLSTR to a 
MATLAB string so that EVAL(STR) produces the original cell-string.
Works as corresponding MAT2STR but for cell array of strings 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.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %Converts a 2-D cell array of strings to a string in MATLAB syntax.
0002 %
0003 %STR = CELL2STR(CELLSTR) converts the 2-D cell-string CELLSTR to a
0004 %MATLAB string so that EVAL(STR) produces the original cell-string.
0005 %Works as corresponding MAT2STR but for cell array of strings 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 
0014 %Developed by Per-Anders Ekström, 2003-2007 Facilia AB.
0015 
0016 function string = cell2str( cellstr )
0017 
0018 if nargin~=1
0019     error('CELL2STR:Nargin','Takes 1 input argument.');
0020 end
0021 if ischar(cellstr)
0022    string = ['''' strrep(cellstr,'''','''''') ''''];
0023    return
0024 end
0025 if ~iscellstr(cellstr)
0026     error('CELL2STR:Class','Input argument must be cell array of strings.');
0027 end
0028 if ndims(cellstr)>2
0029     error('CELL2STR:TwoDInput','Input cell array must be 2-D.');
0030 end
0031 
0032 ncols = size(cellstr,2);
0033 for i=1:ncols-1
0034     cellstr(:,i) = cellfun(@(x)['''' strrep(x,'''','''''') ''','],...
0035         cellstr(:,i),'UniformOutput',false);
0036 end
0037 if ncols>0
0038     cellstr(:,ncols) = cellfun(@(x)['''' strrep(x,'''','''''') ''';'],...
0039         cellstr(:,ncols),'UniformOutput',false);
0040 end
0041 cellstr = cellstr';
0042 string = ['{' cellstr{:} '}'];
0043 
0044 end

Generated on Thu 24-Sep-2009 17:17:32 by m2html © 2005