[309] | 1 | % ----------------------------------------------------------------------- |
---|
[281] | 2 | % --- read a GUI with handle 'handle' producing a structure 'struct' |
---|
[395] | 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 | % |
---|
[281] | 15 | function struct=read_GUI(handle) |
---|
[309] | 16 | %------------------------------------------------------------------------ |
---|
[287] | 17 | struct=[];%default |
---|
[281] | 18 | hchild=get(handle,'children'); |
---|
[379] | 19 | hchild=flipdim(hchild,1);% reverse the order to respect the tab order in the GUI |
---|
[281] | 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 |
---|
[379] | 30 | index=0; |
---|
[281] | 31 | switch object_style |
---|
[363] | 32 | case {'checkbox','radiobutton','togglebutton'} |
---|
[295] | 33 | input=get(hchild(ichild),'Value'); |
---|
[281] | 34 | case 'edit' |
---|
[379] | 35 | separator=regexp(tag,'^num_','once');%look for the prefix 'num_' |
---|
[281] | 36 | if isempty(separator) |
---|
[323] | 37 | input=get(hchild(ichild),'String'); |
---|
[379] | 38 | else %transform into numeric if the edit box begins by the prefix 'num_' |
---|
| 39 | input=str2double(get(hchild(ichild),'String')); |
---|
[363] | 40 | tag=regexprep(tag,'^num_',''); |
---|
[379] | 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 |
---|
[363] | 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)) |
---|
[281] | 51 | end |
---|
[363] | 52 | end |
---|
| 53 | case{'listbox','popupmenu'} |
---|
[295] | 54 | listinput=get(hchild(ichild),'String'); |
---|
| 55 | value=get(hchild(ichild),'Value'); |
---|
[323] | 56 | if ~isempty(listinput) |
---|
[363] | 57 | input=listinput{value}; |
---|
| 58 | else |
---|
| 59 | check_input=0; |
---|
[323] | 60 | end |
---|
[379] | 61 | separator=regexp(tag,'^num_','once'); |
---|
[363] | 62 | if ~isempty(separator) |
---|
[309] | 63 | input=str2double(input);% transform to numerical values if the uicontrol tag begins with 'num_' |
---|
[363] | 64 | tag=regexprep(tag,'^num_',''); |
---|
[309] | 65 | end |
---|
[281] | 66 | otherwise |
---|
| 67 | check_input=0; |
---|
| 68 | end |
---|
[395] | 69 | if check_input% |
---|
[379] | 70 | if index==0 |
---|
| 71 | struct.(tag)=input; |
---|
[395] | 72 | elseif ~isempty(input) |
---|
[379] | 73 | struct.(tag)(index)=input; |
---|
| 74 | end |
---|
[281] | 75 | end |
---|
[350] | 76 | case 'uitable' |
---|
[363] | 77 | struct.(tag)=get(hchild(ichild),'Data'); |
---|
[281] | 78 | end |
---|
| 79 | end |
---|
| 80 | end |
---|