1 | %'fill_GUI': fill a GUI with a set of parameters from a Matlab structure |
---|
2 | % ----------------------------------------------------------------------- |
---|
3 | % function errormsg=fill_GUI(Param,GUI_handle) |
---|
4 | % OUPUT: |
---|
5 | % errormsg: error message, ='' by default |
---|
6 | % |
---|
7 | % INPUT: |
---|
8 | % Param: matlab structure containing the information to display in the GUI |
---|
9 | % GUI_handle: handle of the GUI to be filled |
---|
10 | % |
---|
11 | % see also the reverse function read_GUI.m |
---|
12 | % |
---|
13 | function errormsg=fill_GUI(Param,GUI_handle) |
---|
14 | %------------------------------------------------------------------------ |
---|
15 | errormsg=''; |
---|
16 | if ~isstruct(Param) |
---|
17 | errormsg='first input parmaeter of fill_GUI must be a structure'; |
---|
18 | return |
---|
19 | end |
---|
20 | children=get(GUI_handle,'children');%handles of the children of the input GUI with handle 'GUI_handle' |
---|
21 | handles=[]; |
---|
22 | for ichild=1:numel(children) |
---|
23 | if ~isempty(get(children(ichild),'tag')) |
---|
24 | handles.(get(children(ichild),'tag'))=children(ichild); |
---|
25 | end |
---|
26 | end |
---|
27 | fields=fieldnames(Param);%list of fields in Param |
---|
28 | |
---|
29 | %-------------------------------------------------------------------------------------- |
---|
30 | %----------------- loop on the elements of the input structure Param ------------------ |
---|
31 | %-------------------------------------------------------------------------------------- |
---|
32 | for ifield=1:numel(fields) |
---|
33 | if isstruct(Param.(fields{ifield}))% case of a sub-structure |
---|
34 | %% case of a sub-structure |
---|
35 | % if a panel in the GUI has the tag fields{ifield}, fill it with the sub-structure content |
---|
36 | if isfield(handles,fields{ifield}) |
---|
37 | set(handles.(fields{ifield}),'Visible','on') |
---|
38 | errormsg=fill_GUI(Param.(fields{ifield}),handles.(fields{ifield}));% recursively apply the function to the substructure |
---|
39 | end |
---|
40 | else |
---|
41 | %% case of an element |
---|
42 | hh=[]; |
---|
43 | input_data=Param.(fields{ifield}); |
---|
44 | check_done=0; |
---|
45 | if isfield(handles,fields{ifield}) |
---|
46 | % a GUI element has a tag name equal to the key name in the element of Param |
---|
47 | hh=handles.(fields{ifield}); |
---|
48 | if strcmp(get(hh,'Type'),'uitable') |
---|
49 | % case of a table |
---|
50 | set(hh,'Visible','on') |
---|
51 | if ischar(input_data) |
---|
52 | input_data={input_data};% transform string to a single cell if needed |
---|
53 | end |
---|
54 | set(hh,'Data',input_data) |
---|
55 | check_done=1; |
---|
56 | end |
---|
57 | elseif isnumeric(input_data) |
---|
58 | % for numeric input element, look for a GUI element with the same tag name preceded by 'num_' |
---|
59 | if numel(input_data)>1 % deals with array displayed in multiple boxes labeled by an index |
---|
60 | for ibox=1:numel(input_data) |
---|
61 | if isfield(handles,['num_' fields{ifield} '_' num2str(ibox)]) |
---|
62 | hh(ibox)=handles.(['num_' fields{ifield} '_' num2str(ibox)]); |
---|
63 | end |
---|
64 | end |
---|
65 | else % single box (usual case) |
---|
66 | if isfield(handles,['num_' fields{ifield}]) |
---|
67 | hh=handles.(['num_' fields{ifield}]); |
---|
68 | end |
---|
69 | end |
---|
70 | end |
---|
71 | for ibox=1:numel(hh) |
---|
72 | % finalise the update of GUI uicontrol filled by the input element |
---|
73 | if ~isempty(hh(ibox))&& ~check_done |
---|
74 | set(hh(ibox),'Visible','on')% make the filled GUI element visible |
---|
75 | if isfield(get(hh(ibox)),'Style') |
---|
76 | switch get(hh(ibox),'Style') |
---|
77 | case {'checkbox','radiobutton','togglebutton'} |
---|
78 | if isnumeric(input_data) |
---|
79 | set(hh(ibox),'Value',input_data(ibox)) |
---|
80 | end |
---|
81 | case 'edit' |
---|
82 | input_string=''; |
---|
83 | if isnumeric(input_data) |
---|
84 | if numel(input_data)>0 |
---|
85 | input_string=num2str(input_data(ibox),4); |
---|
86 | end |
---|
87 | else |
---|
88 | input_string=input_data; |
---|
89 | end |
---|
90 | set(hh(ibox),'String',input_string) |
---|
91 | case{'listbox','popupmenu'} |
---|
92 | if isnumeric(input_data) |
---|
93 | input_data=num2str(input_data,4); |
---|
94 | end |
---|
95 | menu=get(hh(ibox),'String'); |
---|
96 | if ischar(input_data) |
---|
97 | input_data={input_data}; |
---|
98 | end |
---|
99 | values=zeros(size(input_data)); |
---|
100 | for idata=1:numel(input_data) |
---|
101 | iline=find(strcmp(input_data{idata},menu)); |
---|
102 | if isempty(iline) |
---|
103 | values(idata)=1; |
---|
104 | menu=[input_data(idata);menu]; |
---|
105 | else |
---|
106 | values(idata)=iline(1); |
---|
107 | end |
---|
108 | end |
---|
109 | set(hh(ibox),'String',menu) |
---|
110 | set(hh(ibox),'Value',values) |
---|
111 | end |
---|
112 | end |
---|
113 | end |
---|
114 | end |
---|
115 | end |
---|
116 | end |
---|