comparison m-toolbox/classes/+utils/@prog/cell2str.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 function string = cell2str(cellstr)
2 %CELL2STR Convert a 2-D cell array to a string in MATLAB syntax.
3 % STR = CELL2STR(CELLSTR) converts the 2-D CELLSTR to a
4 % MATLAB string so that EVAL(STR) produces the original cell-array.
5 % Works as corresponding MAT2STR but for cell array instead of
6 % scalar matrices.
7 %
8 % Example
9 % cellstr = {'U-234','Th-230'};
10 % cell2str(cellstr) produces the string '{''U-234'',''Th-230'';}'.
11 %
12 % See also MAT2STR, STRREP, CELLFUN, EVAL.
13 % Developed by Per-Anders Ekstr?m, 2003-2007 Facilia AB.
14 %
15 % Modified by Nicola Tateo for the LTPDA toolbox, to work also with cell
16 % arrays of numbers and to remove the last ';'
17 %
18 % $Id: cell2str.m,v 1.3 2009/07/15 14:09:42 hewitson Exp $
19 %
20
21 if nargin~=1
22 error('CELL2STR:Nargin','Takes 1 input argument.');
23 end
24 if ischar(cellstr)
25 string = ['''' strrep(cellstr,'''','''''') ''''];
26 return
27 end
28 if ndims(cellstr)>2
29 error('CELL2STR:TwoDInput','Input cell array must be 2-D.');
30 end
31
32 if isempty(cellstr)
33 string = '{}';
34 else
35 if iscellstr(cellstr)
36 ncols = size(cellstr,2);
37 for i=1:ncols-1
38 output(:,i) = cellfun(@(x)['''' strrep(x,'''','''''') ''', '],...
39 cellstr(:,i),'UniformOutput',false);
40 end
41 if ncols>0
42 output(:,ncols) = cellfun(@(x)['''' strrep(x,'''','''''') ''';'],...
43 cellstr(:,ncols),'UniformOutput',false);
44 end
45 output = output';
46 output{numel(output)}(numel(output{numel(output)})) = [];
47 string = ['{' output{:} '}'];
48 else
49 output = mat2str(cell2mat(cellstr));
50 if numel(output)>1, string = ['{',output(2:numel(output)-1),'}'];
51 else string = ['{',output,'}'];
52 end
53 end
54 end
55