1 | function Output1 = addmenustruct(InputStruct, h, Command, LabelString, ParentLabelString) |
---|
2 | %ADDMENUSTRUCT - Adds a menu bar to view and edit a structure |
---|
3 | % addmenustruct(handle) |
---|
4 | % |
---|
5 | % Written by Greg Portmann |
---|
6 | |
---|
7 | Output1 = []; |
---|
8 | |
---|
9 | if nargin < 1 |
---|
10 | error('Structure input required'); |
---|
11 | end |
---|
12 | if nargin < 2 |
---|
13 | h = gcf; |
---|
14 | end |
---|
15 | if nargin < 3 |
---|
16 | Command = 'Build'; |
---|
17 | end |
---|
18 | |
---|
19 | |
---|
20 | switch lower(Command) |
---|
21 | case 'build' |
---|
22 | |
---|
23 | if nargin < 4 |
---|
24 | LabelString = inputname(1); |
---|
25 | end |
---|
26 | if nargin < 5 |
---|
27 | ParentLabelString = 'Edit'; |
---|
28 | end |
---|
29 | |
---|
30 | if strcmpi(get(h,'Type'), 'figure') |
---|
31 | h_childen = get(gcf,'children'); |
---|
32 | EditMenuFlag = 0; |
---|
33 | for i = 1:length(h_childen) |
---|
34 | if strcmpi(get(h_childen,'label'), ParentLabelString) |
---|
35 | EditMenuFlag = 1; |
---|
36 | h = h_childen(i); |
---|
37 | break; |
---|
38 | end |
---|
39 | end |
---|
40 | if ~EditMenuFlag |
---|
41 | % Make an edit menu |
---|
42 | h = uimenu(h, 'Label', ParentLabelString, 'Callback',''); |
---|
43 | end |
---|
44 | hmenu = uimenu(h, 'Label', LabelString, 'Callback',''); |
---|
45 | elseif strcmpi(get(h,'Type'), 'uimenu') |
---|
46 | if isempty(findstr(get(h, 'Label'), 'Middle Layer')) |
---|
47 | hmenu = uimenu(h, 'Label', LabelString, 'Callback',''); |
---|
48 | else |
---|
49 | hmenu = h; |
---|
50 | end |
---|
51 | else |
---|
52 | hmenu = h; |
---|
53 | end |
---|
54 | |
---|
55 | Output1 = hmenu; |
---|
56 | |
---|
57 | DataStructure0 = getad; |
---|
58 | if isempty(DataStructure0) |
---|
59 | error('Middle layer initialization needed'); |
---|
60 | end |
---|
61 | |
---|
62 | |
---|
63 | % Build middle menu tree |
---|
64 | DataStructFieldName1 = fieldnames(DataStructure0); |
---|
65 | for i = 1:length(DataStructFieldName1) |
---|
66 | DataStructure1 = DataStructure0.(DataStructFieldName1{i}); |
---|
67 | if isstruct(DataStructure1) |
---|
68 | hmenu1 = uimenu(hmenu, 'Label',DataStructFieldName1{i}, 'Callback',''); |
---|
69 | DataStructFieldName2 = fieldnames(DataStructure1); |
---|
70 | for j = 1:length(DataStructFieldName2) |
---|
71 | DataStructure2 = DataStructure1.(DataStructFieldName2{j}); |
---|
72 | if isstruct(DataStructure2) |
---|
73 | hmenu2 = uimenu(hmenu1, 'Label',DataStructFieldName2{j}, 'Callback',''); |
---|
74 | DataStructFieldName3 = fieldnames(DataStructure2); |
---|
75 | for k = 1:length(DataStructFieldName3) |
---|
76 | DataStructure3 = DataStructure2.(DataStructFieldName3{k}); |
---|
77 | if isstruct(DataStructure3) |
---|
78 | hmenu3 = uimenu(hmenu2, 'Label',DataStructFieldName3{k}, 'Callback',''); |
---|
79 | DataStructFieldName4 = fieldnames(DataStructure3); |
---|
80 | for l = 1:length(DataStructFieldName4) |
---|
81 | hmenu4 = uimenu(hmenu3, 'Label', DataStructFieldName4{l}); |
---|
82 | set(hmenu4,'UserData', {DataStructure3.(DataStructFieldName4{l}) , DataStructFieldName1{l}, DataStructFieldName2{k}, DataStructFieldName3{k}, DataStructFieldName4{i}}); |
---|
83 | set(hmenu4,'Callback', 'addmenustruct(gcbo,''Action'')'); |
---|
84 | end |
---|
85 | else |
---|
86 | hmenu3 = uimenu(hmenu2, 'Label',DataStructFieldName3{k}); |
---|
87 | set(hmenu3,'UserData', {DataStructure3, DataStructFieldName1{i}, DataStructFieldName2{j}, DataStructFieldName3{k}}); |
---|
88 | set(hmenu3,'Callback', 'addmenustruct(gcbo,''Action'')'); |
---|
89 | end |
---|
90 | end |
---|
91 | else |
---|
92 | hmenu2 = uimenu(hmenu1, 'Label',DataStructFieldName2{j}); |
---|
93 | set(hmenu2,'UserData', {DataStructure2, DataStructFieldName1{i}, DataStructFieldName2{j}}); |
---|
94 | set(hmenu2,'Callback', 'addmenustruct(gcbo,''Action'')'); |
---|
95 | end |
---|
96 | end |
---|
97 | else |
---|
98 | hmenu1 = uimenu(hmenu, 'Label', DataStructFieldName1{i}); |
---|
99 | set(hmenu1,'UserData', {DataStructure1, DataStructFieldName1{i}}); |
---|
100 | set(hmenu1,'Callback', 'addmenustruct(gcbo,''Action'')'); |
---|
101 | end |
---|
102 | end |
---|
103 | |
---|
104 | case 'action' |
---|
105 | |
---|
106 | Data = get(h, 'UserData'); |
---|
107 | %Label = get(h, 'Label'); |
---|
108 | TitleString = sprintf('%s ',Data{2:end}); |
---|
109 | AddOpts.Resize='on'; |
---|
110 | AddOpts.WindowStyle='normal'; |
---|
111 | AddOpts.Interpreter='tex'; |
---|
112 | |
---|
113 | if isnumeric(Data{1}) |
---|
114 | answer = inputdlg({sprintf('Change %s',TitleString)}, 'Edit Middle Layer Parameter', size(Data{1},1), {num2str(Data{1})}, AddOpts); |
---|
115 | if isempty(answer) |
---|
116 | return |
---|
117 | end |
---|
118 | Data{1} = str2num(answer{1}); |
---|
119 | set(h, 'UserData', Data); |
---|
120 | %setfamilydata(Data{1:end}); |
---|
121 | elseif strcmpi(Data{2},'Directory') |
---|
122 | answer = uigetdir(Data{1}, sprintf('Change directory location for %s', Data{3})); |
---|
123 | if answer == 0 |
---|
124 | return |
---|
125 | end |
---|
126 | Data{1} = answer; |
---|
127 | set(h, 'UserData', Data); |
---|
128 | %setfamilydata(Data{1:end}); |
---|
129 | elseif ischar(Data{1}) |
---|
130 | answer = inputdlg({sprintf('Change %s',TitleString)}, 'Edit Middle Layer Parameter', size(Data{1},1), {num2str(Data{1})}, AddOpts); |
---|
131 | if isempty(answer) |
---|
132 | return |
---|
133 | end |
---|
134 | Data{1} = answer{1}; |
---|
135 | set(h, 'UserData', Data); |
---|
136 | %setfamilydata(Data{1:end}); |
---|
137 | end |
---|
138 | |
---|
139 | case {'noaction', 'no action'} |
---|
140 | end |
---|