Repository navigation
Expand file tree
/
Copy pathsaveStruct.m
More file actions
72 lines (66 loc) · 1.69 KB
/
Copy pathsaveStruct.m
File metadata and controls
72 lines (66 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function saveStruct(fid,root,varargin)
%saves the root structure to the file with file identifier fid.
%to display the output to the screen set fid=1
%Note: the root node of a structure is not saved as a field to fid.
%See the second example on how to save the root node if desired.
%
%
%example:
%p.x = 1;
%p.z.b = 'hi'
%fid = fopen(filename,'w');
%saveStruct(fid,p);
%fclose(fid);
%
%will save the following to the file named filename
% x 1
% z.b 'hi'
%
%if the above example is altered as such
%
% p.x = 1;
% p.z.b = 'hi'
% tmp.p = p;
% fid = fopen(filename,'w');
% saveStruct(fid,tmp);
% fclose(fid);
%
%the following output is saved to the file named filename
%
% p.x 1
% p.z.b 'hi'
%
%use loadStruct to recover a structure that has been saved with this
%function.
%
%Author: Hyatt Moore IV
%21JULY2010
%Stanford University
if(isempty(varargin))
if(isstruct(root))
fields = fieldnames(root);
for k=1:numel(fields)
saveStruct(fid,root,deblank(fields{k}));
fprintf(fid,'\r'); %this adds extra line between root groups.
end;
else
fprintf(fid,'root %s\r',num2str(root));
end;
else
field = getfield(root,varargin{:});
if(isstruct(field))
fields = fieldnames(getfield(root,varargin{:}));
for k=1:numel(fields)
saveStruct(fid,root,varargin{:},fields{k});
end;
else
fprintf(fid,'%s\t%s\r',strcat_with_dot(varargin{:}),num2str(field));
end;
end;
function out_str = strcat_with_dot(root,varargin)
%like strcat, except here a '.' is placed in between each element
if(isempty(varargin))
out_str = root;
else
out_str = strcat(root,'.',strcat_with_dot(varargin{:}));
end;