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 | % OUTPUT: |
---|
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 | %======================================================================= |
---|
14 | % Copyright 2008-2021, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
15 | % http://www.legi.grenoble-inp.fr |
---|
16 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
---|
17 | % |
---|
18 | % This file is part of the toolbox UVMAT. |
---|
19 | % |
---|
20 | % UVMAT is free software; you can redistribute it and/or modify |
---|
21 | % it under the terms of the GNU General Public License as published |
---|
22 | % by the Free Software Foundation; either version 2 of the license, |
---|
23 | % or (at your option) any later version. |
---|
24 | % |
---|
25 | % UVMAT is distributed in the hope that it will be useful, |
---|
26 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
27 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
28 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
29 | %======================================================================= |
---|
30 | |
---|
31 | function errormsg=fill_GUI(Param,GUI_handle) |
---|
32 | %------------------------------------------------------------------------ |
---|
33 | errormsg=''; |
---|
34 | if ~isstruct(Param) |
---|
35 | errormsg='first input parmaeter of fill_GUI must be a structure'; |
---|
36 | return |
---|
37 | end |
---|
38 | children=get(GUI_handle,'children');%handles of the children of the input GUI with handle 'GUI_handle' |
---|
39 | handles=[]; |
---|
40 | for ichild=1:numel(children) |
---|
41 | if ~isempty(get(children(ichild),'tag')) |
---|
42 | handles.(get(children(ichild),'tag'))=children(ichild); |
---|
43 | end |
---|
44 | end |
---|
45 | fields=fieldnames(Param);%list of fields in Param |
---|
46 | |
---|
47 | %-------------------------------------------------------------------------------------- |
---|
48 | %----------------- loop on the elements of the input structure Param ------------------ |
---|
49 | %-------------------------------------------------------------------------------------- |
---|
50 | for ifield=1:numel(fields) |
---|
51 | if isstruct(Param.(fields{ifield}))% case of a sub-structure |
---|
52 | %% case of a sub-structure |
---|
53 | % if a panel in the GUI has the tag fields{ifield}, fill it with the sub-structure content |
---|
54 | if isfield(handles,fields{ifield}) |
---|
55 | set(handles.(fields{ifield}),'Visible','on') |
---|
56 | errormsg=fill_GUI(Param.(fields{ifield}),handles.(fields{ifield}));% recursively apply the function to the substructure |
---|
57 | end |
---|
58 | else |
---|
59 | %% case of an element |
---|
60 | hh=[]; |
---|
61 | input_data=Param.(fields{ifield}); |
---|
62 | check_done=0; |
---|
63 | if isfield(handles,fields{ifield}) |
---|
64 | % a GUI element has a tag name equal to the key name in the element of Param |
---|
65 | hh=handles.(fields{ifield}); |
---|
66 | if strcmp(get(hh,'Type'),'uitable') |
---|
67 | % case of a table |
---|
68 | set(hh,'Visible','on') |
---|
69 | if ischar(input_data) |
---|
70 | input_data={input_data};% transform string to a single cell if needed |
---|
71 | end |
---|
72 | set(hh,'Data',input_data) |
---|
73 | check_done=1; |
---|
74 | end |
---|
75 | elseif isnumeric(input_data) |
---|
76 | % for numeric input element, look for a GUI element with the same tag name preceded by 'num_' |
---|
77 | if numel(input_data)>1 % deals with array displayed in multiple boxes labeled by an index |
---|
78 | for ibox=1:numel(input_data) |
---|
79 | if isfield(handles,['num_' fields{ifield} '_' num2str(ibox)]) |
---|
80 | hh(ibox)=handles.(['num_' fields{ifield} '_' num2str(ibox)]); |
---|
81 | end |
---|
82 | end |
---|
83 | else % single box (usual case) |
---|
84 | if isfield(handles,['num_' fields{ifield}]) |
---|
85 | hh=handles.(['num_' fields{ifield}]); |
---|
86 | end |
---|
87 | end |
---|
88 | end |
---|
89 | for ibox=1:numel(hh) |
---|
90 | % finalise the update of GUI uicontrol filled by the input element |
---|
91 | if ~isempty(hh(ibox))&& ~check_done |
---|
92 | set(hh(ibox),'Visible','on')% make the filled GUI element visible |
---|
93 | if isfield(get(hh(ibox)),'Style') |
---|
94 | switch get(hh(ibox),'Style') |
---|
95 | case {'checkbox','radiobutton','togglebutton'} |
---|
96 | if isnumeric(input_data) |
---|
97 | set(hh(ibox),'Value',input_data(ibox)) |
---|
98 | end |
---|
99 | case 'edit' |
---|
100 | input_string=''; |
---|
101 | if isnumeric(input_data) |
---|
102 | if numel(input_data)>0 |
---|
103 | input_string=num2str(input_data(ibox),4); |
---|
104 | end |
---|
105 | else |
---|
106 | input_string=input_data; |
---|
107 | end |
---|
108 | set(hh(ibox),'String',input_string) |
---|
109 | case {'listbox','popupmenu'} |
---|
110 | if isnumeric(input_data) |
---|
111 | input_data=num2str(input_data,4); |
---|
112 | end |
---|
113 | menu=get(hh(ibox),'String'); |
---|
114 | if ischar(input_data) |
---|
115 | input_data={input_data}; |
---|
116 | end |
---|
117 | values=zeros(size(input_data)); |
---|
118 | for idata=1:numel(input_data) |
---|
119 | iline=find(strcmp(input_data{idata},menu)); |
---|
120 | if isempty(iline) |
---|
121 | values(idata)=1; |
---|
122 | menu=[input_data(idata);menu]; |
---|
123 | else |
---|
124 | values(idata)=iline(1); |
---|
125 | end |
---|
126 | end |
---|
127 | set(hh(ibox),'String',menu) |
---|
128 | set(hh(ibox),'Value',values) |
---|
129 | end |
---|
130 | end |
---|
131 | end |
---|
132 | end |
---|
133 | end |
---|
134 | end |
---|