-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructree.m
61 lines (54 loc) · 2.75 KB
/
structree.m
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
function structree(structure,file_name)
%STRUCTREE Prints to a file the fields of a structure in tree form.
%
% STRUCTREE(STRUCTURE,FILE_NAME) prints all the fieldnames of the
% specified structure in a file. By default, the file will be saved
% in the Matlab's currrent directory.
% 2004Apr23 Created by Juan E. Vega
level=1; %variable initialization
row=[1 1];
structure(level,1).data=structure; %structure initialization
fid = fopen(file_name,'w'); %open file
while ~isequal(level,0);
flag=isa(structure(level,1).data,'struct'); %check that current structure is really a structure
if (flag==1);
if(row(level)==1); %do this only if you are accesing this level for the first time
Temp_names=fieldnames(structure(level,1).data); %get field names
Temp_size=size(Temp_names); %get number of fields
Directory_size(level)=Temp_size(1); %store number of fields
Directory_names(1:Directory_size(level),level)=Temp_names; %store field names in corresponding level
end
Display_name=Directory_names{row(level),level}; %Change field name from cell to arrray
for index= 1:level-1; %add necessary tabs
fprintf(fid,'\t|');
end
fprintf(fid,'---');
fprintf(fid,'%s\n',Display_name); %Write name to file
level=level+1; %Go up one level
row(level+1)=1; %Initialize row variable
structure(level,1).data=structure(level-1,1).data.(Display_name); %Update current structure
else
level=level-1; %If current sutructure is not really a structure go down one level
row(level)=row(level)+1; %Go to next row
feedline=0;
while (row(level)>Directory_size(level)); %Do this if row number exceeds the number of fields
level=level-1; %Go down one level
%for index= 1:level-1; %add necessary tabs
% fprintf(fid,'\t|');
%end
if(feedline==0)
fprintf(fid,'\n');
feedline=1;
end
if (level==0);
break;
end
row(level+1)=1;
row(level)=row(level)+1;
end
if (level>1);
structure(level,1).data=getfield(structure(level-1,1).data,Directory_names{row(level-1),level-1}); %Update structure
end
end
end
status = fclose(fid); %Close file