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-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
15 | % http://www.legi.grenoble-inp.fr |
---|
16 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.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 | if isfield(Param,'Type') && isa(Param.Type,'matlab.graphics.primitive.Rectangle') |
---|
63 | input_data='rectangle'; |
---|
64 | end |
---|
65 | check_done=0; |
---|
66 | if isfield(handles,fields{ifield}) |
---|
67 | % a GUI element has a tag name equal to the key name in the element of Param |
---|
68 | hh=handles.(fields{ifield}); |
---|
69 | if strcmp(get(hh,'Type'),'uitable') |
---|
70 | % case of a table |
---|
71 | set(hh,'Visible','on') |
---|
72 | if ischar(input_data) |
---|
73 | input_data={input_data};% transform string to a single cell if needed |
---|
74 | end |
---|
75 | set(hh,'Data',input_data) |
---|
76 | check_done=1; |
---|
77 | end |
---|
78 | elseif isnumeric(input_data) |
---|
79 | % for numeric input element, look for a GUI element with the same tag name preceded by 'num_' |
---|
80 | if numel(input_data)>1 % deals with array displayed in multiple boxes labeled by an index |
---|
81 | for ibox=1:numel(input_data) |
---|
82 | if isfield(handles,['num_' fields{ifield} '_' num2str(ibox)]) |
---|
83 | hh(ibox)=handles.(['num_' fields{ifield} '_' num2str(ibox)]); |
---|
84 | end |
---|
85 | end |
---|
86 | else % single box (usual case) |
---|
87 | if isfield(handles,['num_' fields{ifield}]) |
---|
88 | hh=handles.(['num_' fields{ifield}]); |
---|
89 | end |
---|
90 | end |
---|
91 | end |
---|
92 | for ibox=1:numel(hh) |
---|
93 | % finalise the update of GUI uicontrol filled by the input element |
---|
94 | if ~isempty(hh(ibox))&& ~check_done |
---|
95 | set(hh(ibox),'Visible','on')% make the filled GUI element visible |
---|
96 | if isfield(get(hh(ibox)),'Style') |
---|
97 | switch get(hh(ibox),'Style') |
---|
98 | case {'checkbox','radiobutton','togglebutton'} |
---|
99 | if isnumeric(input_data) |
---|
100 | set(hh(ibox),'Value',input_data(ibox)) |
---|
101 | end |
---|
102 | case 'edit' |
---|
103 | input_string=''; |
---|
104 | if isnumeric(input_data) |
---|
105 | if numel(input_data)>0 |
---|
106 | input_string=num2str(input_data(ibox),4); |
---|
107 | end |
---|
108 | elseif ischar(input_data) |
---|
109 | input_string=input_data; |
---|
110 | end |
---|
111 | set(hh(ibox),'String',input_string) |
---|
112 | case {'listbox','popupmenu'} |
---|
113 | if isnumeric(input_data) |
---|
114 | input_data=num2str(input_data,4); |
---|
115 | end |
---|
116 | menu=get(hh(ibox),'String'); |
---|
117 | if ischar(input_data) |
---|
118 | input_data={input_data}; |
---|
119 | end |
---|
120 | values=zeros(size(input_data)); |
---|
121 | for idata=1:numel(input_data) |
---|
122 | iline=find(strcmp(input_data{idata},menu)); |
---|
123 | if isempty(iline) |
---|
124 | values(idata)=1; |
---|
125 | menu=[input_data(idata);menu]; |
---|
126 | else |
---|
127 | values(idata)=iline(1); |
---|
128 | end |
---|
129 | end |
---|
130 | set(hh(ibox),'String',menu) |
---|
131 | set(hh(ibox),'Value',values) |
---|
132 | end |
---|
133 | end |
---|
134 | end |
---|
135 | end |
---|
136 | end |
---|
137 | end |
---|