|
1 %====================================================================== |
|
2 % S T R U C T 2 A N T S . M |
|
3 % doc: Thu Oct 20 11:48:17 2005 |
|
4 % dlm: Tue Apr 15 14:43:45 2008 |
|
5 % (c) 2005 A.M. Thurnherr |
|
6 % uE-Info: 16 56 NIL 0 0 72 2 2 4 NIL ofnI |
|
7 %====================================================================== |
|
8 % |
|
9 % export Matlab structure to ANTS file |
|
10 % |
|
11 % USAGE: struct2ANTS(struct,outFileName) |
|
12 % |
|
13 % NOTES: |
|
14 % - scalar strings & numbers become %PARAMs |
|
15 % - row and column vectors (which can be mixed) become FIELDs |
|
16 % - incompatible vectors, as well as other data types, are skipped |
|
17 |
|
18 % HISTORY: |
|
19 % Oct 20, 2005: - created |
|
20 % Nov 30, 2005: - BUG: string %PARAMs have to be quoted, e.g. if containing + |
|
21 % Apr 19, 2006: - changed output number format according to $# in perlvar(1) |
|
22 % Nov 6, 2006: - made output into doubles for increased precision |
|
23 % Nov 16, 2007: - replaced cell2mat() by char() |
|
24 % Apr 15, 2008: - added vector-compatibility test |
|
25 % - cosmetics |
|
26 |
|
27 function [] = struct2ANTS(struct,ofn) |
|
28 [ldef,dta] = parseStruct(struct,'','',[],ofn); |
|
29 save /tmp/.struct2ANTS dta -ASCII -DOUBLE; |
|
30 system(sprintf('list -M%%.15g %s /tmp/.struct2ANTS > %s',ldef,ofn)); |
|
31 |
|
32 %====================================================================== |
|
33 |
|
34 function [ldef,dta] = parseStruct(struct,ldef,fpref,dta,ofn); |
|
35 |
|
36 fname = fieldnames(struct); |
|
37 for i=1:length(fname) |
|
38 fns = char(fname(i)); |
|
39 f = getfield(struct,fns); |
|
40 if isstruct(f) |
|
41 [ldef,dta] = parseStruct(f,ldef,[fns '.'],dta,ofn); |
|
42 elseif ischar(f) |
|
43 ldef = sprintf('%%%s%s="\\"%s\\"" %s',fpref,fns,f,ldef); |
|
44 elseif isnumeric(f) |
|
45 [r c] = size(f); |
|
46 if r+c == 2 |
|
47 ldef = sprintf('%%%s%s=%.15g %s',fpref,fns,f,ldef); |
|
48 else |
|
49 ldef = sprintf('%s%s%s= ',ldef,fpref,fns); |
|
50 if isempty(dta), ndta = max(r,c); |
|
51 else, ndta = size(dta,1); |
|
52 end |
|
53 if r==1 && length(f)==ndta, dta = [dta,f']; |
|
54 elseif c==1 && length(f)==ndta, dta = [dta,f]; |
|
55 else, disp(sprintf('%s: incompatible %d x %d array %s skipped',ofn,r,c,fns)); |
|
56 end |
|
57 end |
|
58 else |
|
59 disp(sprintf('%s: field %s%s skipped',ofn,fpref,fns)); |
|
60 end |
|
61 end |
|
62 |
|
63 |
|
64 |