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 | function struct=read_GUI(handle) |
---|
17 | %------------------------------------------------------------------------ |
---|
18 | struct=[];%default |
---|
19 | hchild=get(handle,'children'); |
---|
20 | hchild=flipdim(hchild,1);% reverse the order to respect the tab order in the GUI |
---|
21 | for ichild=1:numel(hchild) |
---|
22 | if strcmp(get(hchild(ichild),'Visible'),'on') |
---|
23 | object_type=get(hchild(ichild),'Type'); |
---|
24 | tag=get(hchild(ichild),'tag'); |
---|
25 | switch object_type |
---|
26 | case 'uipanel' |
---|
27 | eval(['struct.' tag '=read_GUI(hchild(ichild));']) |
---|
28 | case 'uicontrol' |
---|
29 | object_style=get(hchild(ichild),'Style'); |
---|
30 | check_input=1;%default |
---|
31 | index=0; |
---|
32 | switch object_style |
---|
33 | case {'checkbox','radiobutton','togglebutton'} |
---|
34 | input=get(hchild(ichild),'Value'); |
---|
35 | case 'edit' |
---|
36 | separator=regexp(tag,'^num_','once');%look for the prefix 'num_' |
---|
37 | if isempty(separator) |
---|
38 | input=get(hchild(ichild),'String'); |
---|
39 | else %transform into numeric if the edit box begins by the prefix 'num_' |
---|
40 | input=str2double(get(hchild(ichild),'String')); |
---|
41 | tag=regexprep(tag,'^num_',''); |
---|
42 | % detect tag name ending by an index: then interpret the input as array(index) |
---|
43 | r=regexp(tag,'_(?<index>\d+)$','names');% detect tag name ending by an index |
---|
44 | if ~isempty(r) |
---|
45 | tag=regexprep(tag,['_' r.index '$'],''); |
---|
46 | index=str2double(r.index); |
---|
47 | end |
---|
48 | %deal with undefined input: retrieve the default value stored as UserData |
---|
49 | if isnan(input) |
---|
50 | input=get(hchild(ichild),'UserData'); |
---|
51 | set(hchild(ichild),'String',num2str(input)) |
---|
52 | end |
---|
53 | end |
---|
54 | case{'listbox','popupmenu'} |
---|
55 | listinput=get(hchild(ichild),'String'); |
---|
56 | value=get(hchild(ichild),'Value'); |
---|
57 | if ~isempty(listinput) |
---|
58 | if numel(value)==1% single selection |
---|
59 | if ischar(listinput) |
---|
60 | input=listinput; |
---|
61 | else |
---|
62 | input=listinput{value}; |
---|
63 | end |
---|
64 | else % multiple selection |
---|
65 | input=listinput(value); |
---|
66 | end |
---|
67 | else |
---|
68 | check_input=0; |
---|
69 | end |
---|
70 | separator=regexp(tag,'^num_','once'); |
---|
71 | if ~isempty(separator) |
---|
72 | input=str2double(input);% transform to numerical values if the uicontrol tag begins with 'num_' |
---|
73 | tag=regexprep(tag,'^num_',''); |
---|
74 | end |
---|
75 | otherwise |
---|
76 | check_input=0; |
---|
77 | end |
---|
78 | if check_input% |
---|
79 | if index==0 |
---|
80 | struct.(tag)=input; |
---|
81 | elseif ~isempty(input) |
---|
82 | struct.(tag)(index)=input; |
---|
83 | end |
---|
84 | end |
---|
85 | case 'uitable' |
---|
86 | struct.(tag)=get(hchild(ichild),'Data'); |
---|
87 | end |
---|
88 | end |
---|
89 | end |
---|
90 | % read UserData if relevant |
---|
91 | UserData=get(handle,'UserData'); |
---|
92 | if isstruct(UserData) |
---|
93 | List=fields(UserData); |
---|
94 | for ilist=1:numel(List) |
---|
95 | if isstruct(UserData.(List{ilist}))% look for edit box with the tag UserData.(List{ilist}) |
---|
96 | heditbox=findobj(handle,'Tag',List{ilist},'Style','edit','Visible','on'); |
---|
97 | if isequal(numel(heditbox),1) |
---|
98 | struct.(List{ilist})=UserData.(List{ilist}); |
---|
99 | else% look for pushbutton with the tag UserData.(List{ilist}) |
---|
100 | hpushbutton=findobj(handle,'Tag',List{ilist},'Style','pushbutton','Visible','on'); |
---|
101 | if isequal(numel(hpushbutton),1) |
---|
102 | struct.(List{ilist})=UserData.(List{ilist}); |
---|
103 | end |
---|
104 | end |
---|
105 | end |
---|
106 | end |
---|
107 | end |
---|