1 | % ----------------------------------------------------------------------- |
---|
2 | % --- read a GUI with handle 'handle' producing a structure 'struct' |
---|
3 | % |
---|
4 | % The output Matlab structure 'struct' contains the information displayed on |
---|
5 | % the GUI or a panel with handle 'handle' |
---|
6 | % The content of a panel with tag 'tag' is displayed as a substructure struct.(tag) (recursive use of read_GUI) |
---|
7 | % Output of an element with tag 'tag': |
---|
8 | % 'checkbox','radiobutton','togglebutton': struct.(tag)=value |
---|
9 | % 'edit': struct.(tag)=string, |
---|
10 | % or, if the tag is in the form by 'num_tag', |
---|
11 | % struct.(tag)=str2double(string). If the result is empty the 'UserData' is taken as the default input. |
---|
12 | % 'listbox','popupmenu': struct.(tag)=selected string, or, if the tag is in the form by 'num_tag', struct.(tag)=str2double(string) |
---|
13 | % 'table': struct.(tag)=data of the table |
---|
14 | % |
---|
15 | function struct=read_GUI(handle) |
---|
16 | %------------------------------------------------------------------------ |
---|
17 | struct=[];%default |
---|
18 | hchild=get(handle,'children'); |
---|
19 | hchild=flipdim(hchild,1);% reverse the order to respect the tab order in the GUI |
---|
20 | for ichild=1:numel(hchild) |
---|
21 | if strcmp(get(hchild(ichild),'Visible'),'on') |
---|
22 | object_type=get(hchild(ichild),'Type'); |
---|
23 | tag=get(hchild(ichild),'tag'); |
---|
24 | switch object_type |
---|
25 | case 'uipanel' |
---|
26 | eval(['struct.' tag '=read_GUI(hchild(ichild));']) |
---|
27 | case 'uicontrol' |
---|
28 | object_style=get(hchild(ichild),'Style'); |
---|
29 | check_input=1;%default |
---|
30 | index=0; |
---|
31 | switch object_style |
---|
32 | case {'checkbox','radiobutton','togglebutton'} |
---|
33 | input=get(hchild(ichild),'Value'); |
---|
34 | case 'edit' |
---|
35 | separator=regexp(tag,'^num_','once');%look for the prefix 'num_' |
---|
36 | if isempty(separator) |
---|
37 | input=get(hchild(ichild),'String'); |
---|
38 | else %transform into numeric if the edit box begins by the prefix 'num_' |
---|
39 | input=str2double(get(hchild(ichild),'String')); |
---|
40 | tag=regexprep(tag,'^num_',''); |
---|
41 | % detect tag name ending by an index: then interpret the input as array(index) |
---|
42 | r=regexp(tag,'_(?<index>\d+)$','names');% detect tag name ending by an index |
---|
43 | if ~isempty(r) |
---|
44 | tag=regexprep(tag,['_' r.index '$'],''); |
---|
45 | index=str2double(r.index); |
---|
46 | end |
---|
47 | %deal with undefined input: retrieve the default value stored as UserData |
---|
48 | if isnan(input) |
---|
49 | input=get(hchild(ichild),'UserData'); |
---|
50 | set(hchild(ichild),'String',num2str(input)) |
---|
51 | end |
---|
52 | end |
---|
53 | case{'listbox','popupmenu'} |
---|
54 | listinput=get(hchild(ichild),'String'); |
---|
55 | value=get(hchild(ichild),'Value'); |
---|
56 | if ~isempty(listinput) |
---|
57 | input=listinput{value}; |
---|
58 | else |
---|
59 | check_input=0; |
---|
60 | end |
---|
61 | separator=regexp(tag,'^num_','once'); |
---|
62 | if ~isempty(separator) |
---|
63 | input=str2double(input);% transform to numerical values if the uicontrol tag begins with 'num_' |
---|
64 | tag=regexprep(tag,'^num_',''); |
---|
65 | end |
---|
66 | otherwise |
---|
67 | check_input=0; |
---|
68 | end |
---|
69 | if check_input% |
---|
70 | if index==0 |
---|
71 | struct.(tag)=input; |
---|
72 | elseif ~isempty(input) |
---|
73 | struct.(tag)(index)=input; |
---|
74 | end |
---|
75 | end |
---|
76 | case 'uitable' |
---|
77 | struct.(tag)=get(hchild(ichild),'Data'); |
---|
78 | end |
---|
79 | end |
---|
80 | end |
---|