0
|
1 |
%======================================================================
|
|
2 |
% S T R U C T 2 A N T S . M
|
|
3 |
% doc: Thu Oct 20 11:48:17 2005
|
3
|
4 |
% dlm: Fri Jan 6 09:31:38 2012
|
0
|
5 |
% (c) 2005 A.M. Thurnherr
|
3
|
6 |
% uE-Info: 50 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
|
0
|
35 |
|
3
|
36 |
function [] = struct2ANTS(struct,deps,ofn)
|
|
37 |
|
|
38 |
if nargin ~= 3
|
|
39 |
help struct2ANTS
|
|
40 |
return
|
|
41 |
end
|
|
42 |
|
|
43 |
if iscell(deps)
|
|
44 |
dlist = sprintf('%s,',deps{:});
|
|
45 |
else
|
|
46 |
dlist = sprintf('%s',deps);
|
|
47 |
end
|
|
48 |
|
0
|
49 |
[ldef,dta] = parseStruct(struct,'','',[],ofn);
|
3
|
50 |
save /../tmp/.struct2ANTS dta -ASCII -DOUBLE; % ARGH Matlab R11b on Freebsd 8.2 requires this weird path
|
|
51 |
|
1
|
52 |
if length(ofn) == 0
|
3
|
53 |
system(sprintf('list -M%%.15g -d ''%s'' %s /tmp/.struct2ANTS',dlist,ldef));
|
1
|
54 |
else
|
3
|
55 |
system(sprintf('list -M%%.15g -d ''%s'' %s /tmp/.struct2ANTS > %s',dlist,ldef,ofn));
|
1
|
56 |
end
|
0
|
57 |
|
|
58 |
%======================================================================
|
|
59 |
|
|
60 |
function [ldef,dta] = parseStruct(struct,ldef,fpref,dta,ofn);
|
|
61 |
|
|
62 |
fname = fieldnames(struct);
|
|
63 |
for i=1:length(fname)
|
|
64 |
fns = char(fname(i));
|
|
65 |
f = getfield(struct,fns);
|
|
66 |
if isstruct(f)
|
|
67 |
[ldef,dta] = parseStruct(f,ldef,[fns '.'],dta,ofn);
|
|
68 |
elseif ischar(f)
|
3
|
69 |
f = strrep(f,'%','%%'); % must quote %s
|
|
70 |
f = strrep(f,'$','\\\$'); % presumably also $s
|
0
|
71 |
ldef = sprintf('%%%s%s="\\"%s\\"" %s',fpref,fns,f,ldef);
|
|
72 |
elseif isnumeric(f)
|
|
73 |
[r c] = size(f);
|
|
74 |
if r+c == 2
|
|
75 |
ldef = sprintf('%%%s%s=%.15g %s',fpref,fns,f,ldef);
|
|
76 |
else
|
|
77 |
ldef = sprintf('%s%s%s= ',ldef,fpref,fns);
|
|
78 |
if isempty(dta), ndta = max(r,c);
|
|
79 |
else, ndta = size(dta,1);
|
|
80 |
end
|
|
81 |
if r==1 && length(f)==ndta, dta = [dta,f'];
|
|
82 |
elseif c==1 && length(f)==ndta, dta = [dta,f];
|
3
|
83 |
else, 1; %disp(sprintf('%s: incompatible %d x %d array %s skipped',ofn,r,c,fns));
|
0
|
84 |
end
|
|
85 |
end
|
|
86 |
else
|
|
87 |
disp(sprintf('%s: field %s%s skipped',ofn,fpref,fns));
|
|
88 |
end
|
|
89 |
end
|
|
90 |
|
|
91 |
|
|
92 |
|