0
|
1 |
%======================================================================
|
|
2 |
% S T R U C T 2 A N T S . M
|
|
3 |
% doc: Thu Oct 20 11:48:17 2005
|
5
|
4 |
% dlm: Tue Feb 21 14:30:01 2012
|
0
|
5 |
% (c) 2005 A.M. Thurnherr
|
5
|
6 |
% uE-Info: 93 0 NIL 0 0 72 2 2 4 NIL ofnI
|
0
|
7 |
%======================================================================
|
|
8 |
%
|
|
9 |
% export Matlab structure to ANTS file
|
|
10 |
%
|
3
|
11 |
% USAGE: struct2ANTS(struct,dependencies,outFileName)
|
0
|
12 |
%
|
|
13 |
% NOTES:
|
3
|
14 |
% - <dependencies> can be a string of a cell array of strings
|
0
|
15 |
% - scalar strings & numbers become %PARAMs
|
|
16 |
% - row and column vectors (which can be mixed) become FIELDs
|
|
17 |
% - incompatible vectors, as well as other data types, are skipped
|
|
18 |
|
|
19 |
% HISTORY:
|
|
20 |
% Oct 20, 2005: - created
|
|
21 |
% Nov 30, 2005: - BUG: string %PARAMs have to be quoted, e.g. if containing +
|
|
22 |
% Apr 19, 2006: - changed output number format according to $# in perlvar(1)
|
|
23 |
% Nov 6, 2006: - made output into doubles for increased precision
|
|
24 |
% Nov 16, 2007: - replaced cell2mat() by char()
|
|
25 |
% Apr 15, 2008: - added vector-compatibility test
|
|
26 |
% - cosmetics
|
1
|
27 |
% Oct 12, 2009: - added ifn (ANTS 4.0: deps)
|
|
28 |
% Oct 13, 2009: - allowed for NULL ifn, ofn
|
3
|
29 |
% Oct 13, 2010: - allowed for multiple dependencies
|
|
30 |
% - added usage help
|
|
31 |
% Jul 23: 2011: - BUG: empty dep did not work
|
|
32 |
% Jul 24, 2011: - BUG: PARAMS with % did not work
|
|
33 |
% Jul 25, 2011: - commented out diagnostic message about skipped incompatible vectors
|
|
34 |
% Dec 30, 2011: - workaround for Matlab R11b bug: /tmp/foo cannot be written, /../tmp/foo can
|
5
|
35 |
% Feb 20, 2012: - BUG: diagnostic messages did not show field prefices
|
|
36 |
% - BUG: skipped fields ended up in Layout after all
|
|
37 |
% Feb 21, 2012: - removed double quoting of % and $
|
|
38 |
% - manually merged two versions
|
|
39 |
% - re-added diagnostic messages about skipped incompatible vectors
|
0
|
40 |
|
3
|
41 |
function [] = struct2ANTS(struct,deps,ofn)
|
|
42 |
|
|
43 |
if nargin ~= 3
|
|
44 |
help struct2ANTS
|
|
45 |
return
|
|
46 |
end
|
|
47 |
|
|
48 |
if iscell(deps)
|
|
49 |
dlist = sprintf('%s,',deps{:});
|
|
50 |
else
|
|
51 |
dlist = sprintf('%s',deps);
|
|
52 |
end
|
|
53 |
|
0
|
54 |
[ldef,dta] = parseStruct(struct,'','',[],ofn);
|
3
|
55 |
save /../tmp/.struct2ANTS dta -ASCII -DOUBLE; % ARGH Matlab R11b on Freebsd 8.2 requires this weird path
|
|
56 |
|
1
|
57 |
if length(ofn) == 0
|
3
|
58 |
system(sprintf('list -M%%.15g -d ''%s'' %s /tmp/.struct2ANTS',dlist,ldef));
|
1
|
59 |
else
|
3
|
60 |
system(sprintf('list -M%%.15g -d ''%s'' %s /tmp/.struct2ANTS > %s',dlist,ldef,ofn));
|
1
|
61 |
end
|
0
|
62 |
|
|
63 |
%======================================================================
|
|
64 |
|
|
65 |
function [ldef,dta] = parseStruct(struct,ldef,fpref,dta,ofn);
|
|
66 |
|
|
67 |
fname = fieldnames(struct);
|
|
68 |
for i=1:length(fname)
|
|
69 |
fns = char(fname(i));
|
|
70 |
f = getfield(struct,fns);
|
|
71 |
if isstruct(f)
|
|
72 |
[ldef,dta] = parseStruct(f,ldef,[fns '.'],dta,ofn);
|
|
73 |
elseif ischar(f)
|
|
74 |
ldef = sprintf('%%%s%s="\\"%s\\"" %s',fpref,fns,f,ldef);
|
|
75 |
elseif isnumeric(f)
|
|
76 |
[r c] = size(f);
|
|
77 |
if r+c == 2
|
|
78 |
ldef = sprintf('%%%s%s=%.15g %s',fpref,fns,f,ldef);
|
|
79 |
else
|
|
80 |
if isempty(dta), ndta = max(r,c);
|
|
81 |
else, ndta = size(dta,1);
|
|
82 |
end
|
5
|
83 |
if r==1 && length(f)==ndta
|
|
84 |
ldef = sprintf('%s%s%s= ',ldef,fpref,fns);
|
|
85 |
dta = [dta,f'];
|
|
86 |
elseif c==1 && length(f)==ndta
|
|
87 |
ldef = sprintf('%s%s%s= ',ldef,fpref,fns);
|
|
88 |
dta = [dta,f];
|
|
89 |
else
|
|
90 |
disp(sprintf('%s: incompatible %d x %d array %s%s skipped',ofn,r,c,fpref,fns));
|
0
|
91 |
end
|
|
92 |
end
|
|
93 |
else
|
|
94 |
disp(sprintf('%s: field %s%s skipped',ofn,fpref,fns));
|
|
95 |
end
|
|
96 |
end
|
|
97 |
|
|
98 |
|
|
99 |
|