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