comparison core/tools/cell2str.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 %CELL2STR Convert 2-D cell array into evaluable string.
2 % B = CELL2STR(C) returns a B such that C = EVAL(B), under the
3 % following contraits:
4 % - C contains only numeric, logic, string or cell arrays, not
5 % structs nor objects.
6 % - NDIMS(C) = 2.
7 %
8 % B = CELL2STR(C,N) uses N digits of precision for numeric
9 % arrays. N defaults to 15.
10 %
11 % B = CELL2STR(C,'class') and B = CELL2STR(C,N,'class') also
12 % include the class string for the numeric arrays
13 %
14 % See also MAT2STR, EVAL
15
16 % (C) Copyright 2010-2012, All rights reserved.
17 % Cris Luengo, Uppsala, 12 August 2010.
18 % 13 July 2012: extended to work with cell arrays.
19
20 function str = cell2str(c,n,mode)
21
22 if nargin==2
23 if isnumeric(n)
24 mode = '';
25 else
26 mode = n;
27 n = 15;
28 end
29 elseif nargin==1
30 mode = '';
31 n = 15;
32 end
33
34 if ~isnumeric(n) || numel(n)~=1
35 error('Input argument N should be a scalar.')
36 end
37 if ~ischar(mode) || ~any(strcmp(mode,{'','class'}))
38 error('Mode string can only be ''class''.')
39 end
40
41 if iscell(c)
42 N = size(c);
43 if length(N)~=2
44 error('Cell array must be 2-dimensional.')
45 end
46 str = '{';
47 for ii=1:N(1)
48 if ii>1
49 str = [str,';'];
50 end
51 for jj=1:N(2)
52 if jj>1
53 str = [str,','];
54 end
55 str = [str,cell2str(c{ii,jj})];
56 end
57 end
58 str = [str,'}'];
59 elseif isnumeric(c)
60 if ~isempty(mode)
61 str = mat2str(c,n,'class');
62 else
63 str = mat2str(c,n);
64 end
65 elseif ischar(c) || islogical(c)
66 str = mat2str(c);
67 else
68 error('Illegal array in input.')
69 end