1 | % 'read_GUI':read a GUI and provide the data as a Matlab structure |
---|
2 | %---------------------------------------------------------------------- |
---|
3 | % function struct=read_GUI(handle) |
---|
4 | % |
---|
5 | % OUTPUT: |
---|
6 | % struct: matlab structure containing the information displayed in the GUI |
---|
7 | % The content of a panel with tag 'tag' is displayed as a substructure struct.(tag) (recursive use of read_GUI) |
---|
8 | % Output of a GUI element with tag 'tag': |
---|
9 | % -case 'checkbox','radiobutton','togglebutton': struct.(tag)=value |
---|
10 | % -case'edit': struct.(tag)=string, |
---|
11 | % or, if the tag is in the form by 'num_tag', |
---|
12 | % struct.(tag)=str2double(string). If the result is empty the 'UserData' is taken as the default input. |
---|
13 | % -case 'listbox','popupmenu': struct.(tag)=selected string, or, if the tag is in the form by 'num_tag', struct.(tag)=str2double(string) |
---|
14 | % -case 'table': struct.(tag)=data of the table |
---|
15 | |
---|
16 | %======================================================================= |
---|
17 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
18 | % http://www.legi.grenoble-inp.fr |
---|
19 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr |
---|
20 | % |
---|
21 | % This file is part of the toolbox UVMAT. |
---|
22 | % |
---|
23 | % UVMAT is free software; you can redistribute it and/or modify |
---|
24 | % it under the terms of the GNU General Public License as published |
---|
25 | % by the Free Software Foundation; either version 2 of the license, |
---|
26 | % or (at your option) any later version. |
---|
27 | % |
---|
28 | % UVMAT is distributed in the hope that it will be useful, |
---|
29 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
30 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
31 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
32 | %======================================================================= |
---|
33 | |
---|
34 | function struct=read_GUI(handle) |
---|
35 | %------------------------------------------------------------------------ |
---|
36 | struct=[];%default |
---|
37 | hchild=get(handle,'children'); |
---|
38 | hchild=flipdim(hchild,1);% reverse the order to respect the tab order in the GUI |
---|
39 | for ichild=1:numel(hchild) |
---|
40 | if strcmp(get(hchild(ichild),'Visible'),'on') |
---|
41 | object_type=get(hchild(ichild),'Type'); |
---|
42 | tag=get(hchild(ichild),'tag'); |
---|
43 | switch object_type |
---|
44 | case 'uipanel' |
---|
45 | eval(['struct.' tag '=read_GUI(hchild(ichild));']) |
---|
46 | case 'uicontrol' |
---|
47 | object_style=get(hchild(ichild),'Style'); |
---|
48 | check_input=1;%default |
---|
49 | index=0; |
---|
50 | switch object_style |
---|
51 | case {'checkbox','radiobutton','togglebutton'} |
---|
52 | input=get(hchild(ichild),'Value'); |
---|
53 | case 'edit' |
---|
54 | separator=regexp(tag,'^num_','once');%look for the prefix 'num_' |
---|
55 | if isempty(separator) |
---|
56 | input=get(hchild(ichild),'String'); |
---|
57 | else %transform into numeric if the edit box begins by the prefix 'num_' |
---|
58 | input=str2double(get(hchild(ichild),'String')); |
---|
59 | tag=regexprep(tag,'^num_',''); |
---|
60 | % detect tag name ending by an index: then interpret the input as array(index) |
---|
61 | r=regexp(tag,'_(?<index>\d+)$','names');% detect tag name ending by an index |
---|
62 | if ~isempty(r) |
---|
63 | tag=regexprep(tag,['_' r.index '$'],''); |
---|
64 | index=str2double(r.index); |
---|
65 | end |
---|
66 | %deal with undefined input: retrieve the default value stored as UserData |
---|
67 | if isnan(input) |
---|
68 | input=get(hchild(ichild),'UserData'); |
---|
69 | set(hchild(ichild),'String',num2str(input)) |
---|
70 | end |
---|
71 | end |
---|
72 | case {'listbox','popupmenu'} |
---|
73 | listinput=get(hchild(ichild),'String'); |
---|
74 | value=get(hchild(ichild),'Value'); |
---|
75 | if ~isempty(listinput) |
---|
76 | if numel(value)==1% single selection |
---|
77 | if ischar(listinput) |
---|
78 | input=listinput; |
---|
79 | else |
---|
80 | input=listinput{value}; |
---|
81 | end |
---|
82 | else % multiple selection |
---|
83 | input=listinput(value); |
---|
84 | end |
---|
85 | else |
---|
86 | check_input=0; |
---|
87 | end |
---|
88 | separator=regexp(tag,'^num_','once'); |
---|
89 | if ~isempty(separator) |
---|
90 | input=str2double(input);% transform to numerical values if the uicontrol tag begins with 'num_' |
---|
91 | tag=regexprep(tag,'^num_',''); |
---|
92 | end |
---|
93 | otherwise |
---|
94 | check_input=0; |
---|
95 | end |
---|
96 | if check_input && ~isempty(tag)% |
---|
97 | if index==0 |
---|
98 | struct.(tag)=input; |
---|
99 | elseif ~isempty(input) |
---|
100 | struct.(tag)(index)=input; |
---|
101 | end |
---|
102 | end |
---|
103 | case 'uitable' |
---|
104 | struct.(tag)=get(hchild(ichild),'Data'); |
---|
105 | end |
---|
106 | end |
---|
107 | end |
---|
108 | % read UserData if relevant |
---|
109 | UserData=get(handle,'UserData'); |
---|
110 | if isstruct(UserData) |
---|
111 | List=fields(UserData); |
---|
112 | for ilist=1:numel(List) |
---|
113 | if isstruct(UserData.(List{ilist}))% look for edit box with the tag UserData.(List{ilist}) |
---|
114 | heditbox=findobj(handle,'Tag',List{ilist},'Style','edit','Visible','on'); |
---|
115 | if isequal(numel(heditbox),1) |
---|
116 | struct.(List{ilist})=UserData.(List{ilist}); |
---|
117 | else% look for pushbutton with the tag UserData.(List{ilist}) |
---|
118 | hpushbutton=findobj(handle,'Tag',List{ilist},'Style','pushbutton','Visible','on'); |
---|
119 | if isequal(numel(hpushbutton),1) |
---|
120 | struct.(List{ilist})=UserData.(List{ilist}); |
---|
121 | end |
---|
122 | end |
---|
123 | end |
---|
124 | end |
---|
125 | end |
---|