| 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 | if numel(value)==1% single selection |
---|
| 58 | input=listinput{value}; |
---|
| 59 | else % multiple selection |
---|
| 60 | input=listinput(value); |
---|
| 61 | end |
---|
| 62 | else |
---|
| 63 | check_input=0; |
---|
| 64 | end |
---|
| 65 | separator=regexp(tag,'^num_','once'); |
---|
| 66 | if ~isempty(separator) |
---|
| 67 | input=str2double(input);% transform to numerical values if the uicontrol tag begins with 'num_' |
---|
| 68 | tag=regexprep(tag,'^num_',''); |
---|
| 69 | end |
---|
| 70 | otherwise |
---|
| 71 | check_input=0; |
---|
| 72 | end |
---|
| 73 | if check_input% |
---|
| 74 | if index==0 |
---|
| 75 | struct.(tag)=input; |
---|
| 76 | elseif ~isempty(input) |
---|
| 77 | struct.(tag)(index)=input; |
---|
| 78 | end |
---|
| 79 | end |
---|
| 80 | case 'uitable' |
---|
| 81 | struct.(tag)=get(hchild(ichild),'Data'); |
---|
| 82 | end |
---|
| 83 | end |
---|
| 84 | end |
---|