comparison m-toolbox/classes/+utils/@prog/mcell2str.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 % MCELL2STR recursively converts a cell-array to an executable string.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: MCELL2STR recursively converts a cell-array to an executable
5 % string.
6 %
7 % CALL: s = wrapstring(s, n)
8 %
9 % INPUTS: s - String
10 % n - max length of each cell
11 %
12 % OUTPUTS: s - the wrapped cell string
13 %
14 % VERSION: $Id: mcell2str.m,v 1.8 2010/08/18 15:19:30 ingo Exp $
15 %
16 % HISTORY: 05-02-2007 M Hewitson
17 % Creation
18 %
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20 function fstr = mcell2str(c)
21
22 %----- Check the input
23 if ~iscell(c)
24 error('### The input must be a cell but it is from the class [%s]', class(c));
25 end
26
27 if isempty(c)
28 fstr = sprintf('cell(%d,%d)', size(c, 1), size(c,2));
29 else
30 fstr = '{';
31 Nj = size(c,1);
32 Nk = size(c,2);
33 for jj=1:Nj
34 for kk=1:Nk
35 if iscell(c{jj,kk})
36 %----- cell
37 fstr = [fstr utils.prog.mcell2str(c{jj,kk}) ','];
38
39 elseif ischar(c{jj,kk})
40 %----- char
41 fstr = sprintf('%s''%s'', ', fstr, strrep(c{jj, kk}, '''', ''''''));
42
43 elseif isa(c{jj,kk}, 'sym')
44 %----- sym
45 fstr = [fstr 'sym(''' char(c{jj,kk}) '''),'];
46
47 elseif isnumeric(c{jj,kk})
48 %----- numeric
49 fstr = [fstr utils.helper.mat2str(c{jj,kk}) ','];
50
51 elseif islogical(c{jj,kk})
52 %----- logical
53 fstr = [fstr utils.helper.mat2str(c{jj,kk}) ','];
54
55 elseif isjava(c{jj,kk})
56 %----- java
57 if strcmp(class(c{jj,kk}), 'sun.util.calendar.ZoneInfo')
58 fstr = [fstr 'java.util.TimeZone.getTimeZone(''' char(getID(c{jj,kk})) '''),'];
59 else
60 error('### Unknown java object [%s]', class(c{jj,kk}));
61 end
62
63 elseif isa(c{jj,kk}, 'ltpda_obj')
64 %----- ltpda_obj
65
66 if isa(c{jj,kk}, 'history')
67 %----- history
68 cl = history.getObjectClass(c{jj,kk});
69 ncl = numel(c{jj,kk});
70 cstr = '[';
71 for ll=1:ncl
72 cstr = [cstr sprintf('%s(%s)', cl, string(c{jj,kk}(ll).plistUsed)) ', '];
73 end
74 cstr = strtrim(cstr);
75 cstr(end) = ']';
76 fstr = sprintf('%s%s, ', fstr, cstr);
77 else
78 fstr = sprintf('%s%s, ', fstr, string(c{jj,kk}));
79 end
80
81 elseif isstruct(c{jj,kk})
82 %----- structure
83 fn = fieldnames(c{jj,kk});
84 fstr = [fstr 'struct('];
85 for ii=1:numel(fn)
86 obj = c{jj,kk};
87 if ischar(obj.(fn{ii}))
88 fstr = [fstr '''' fn{ii} ''',''' obj.(fn{ii}) ''','];
89 elseif isnumeric(obj.(fn{ii})) || islogical(obj.(fn{ii}))
90 fstr = [fstr '''' fn{ii} ''',' utils.helper.mat2str(obj.(fn{ii})) ','];
91 elseif isa(obj.(fn{ii}), 'sym')
92 fstr = [fstr '''' fn{ii} ''',' 'sym(''' char(obj.(fn{ii})) '''),'];
93 elseif isa(obj.(fn{ii}), 'ltpda_obj')
94 fstr = [fstr '''' fn{ii} ''',' string(obj.(fn{ii})) ','];
95 end
96 end
97 fstr = [fstr(1:end-1) '),'];
98
99 else
100 disp(c{jj,kk});
101 error(['### unknown cell content: ' class(c{jj,kk})]);
102 end
103 end
104 fstr = strtrim(fstr);
105 if fstr(end) == ','
106 fstr = fstr(1:end-1);
107 end
108 fstr = [fstr ';'];
109 end
110
111 fstr = strtrim(fstr);
112 if fstr(end) == ','
113 fstr = fstr(1:end-1);
114 end
115 if fstr(end) == ';'
116 fstr = fstr(1:end-1);
117 end
118 fstr = [fstr '}'];
119 end
120 end