[497] | 1 | % 'read_GUI':read a GUI and provide the data as a Matlab structure |
---|
| 2 | %---------------------------------------------------------------------- |
---|
| 3 | % function struct=read_GUI(handle) |
---|
[395] | 4 | % |
---|
[497] | 5 | % OUTPUT: |
---|
| 6 | % struct: matlab structure containing the information displayed in the GUI |
---|
[395] | 7 | % The content of a panel with tag 'tag' is displayed as a substructure struct.(tag) (recursive use of read_GUI) |
---|
[497] | 8 | % Output of a GUI element with tag 'tag': |
---|
| 9 | % -case 'checkbox','radiobutton','togglebutton': struct.(tag)=value |
---|
| 10 | % -case'edit': struct.(tag)=string, |
---|
[395] | 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. |
---|
[497] | 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 |
---|
[809] | 15 | |
---|
| 16 | %======================================================================= |
---|
[1126] | 17 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
[809] | 18 | % http://www.legi.grenoble-inp.fr |
---|
[1127] | 19 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr |
---|
[809] | 20 | % |
---|
| 21 | % This file is part of the toolbox UVMAT. |
---|
| 22 | % |
---|
| 23 | % UVMAT is free software; you can redistribute it and/or modify |
---|
| 24 | % it under the terms of the GNU General Public License as published |
---|
| 25 | % by the Free Software Foundation; either version 2 of the license, |
---|
| 26 | % or (at your option) any later version. |
---|
| 27 | % |
---|
| 28 | % UVMAT is distributed in the hope that it will be useful, |
---|
| 29 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 30 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 31 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
| 32 | %======================================================================= |
---|
| 33 | |
---|
[281] | 34 | function struct=read_GUI(handle) |
---|
[309] | 35 | %------------------------------------------------------------------------ |
---|
[287] | 36 | struct=[];%default |
---|
[281] | 37 | hchild=get(handle,'children'); |
---|
[379] | 38 | hchild=flipdim(hchild,1);% reverse the order to respect the tab order in the GUI |
---|
[281] | 39 | for ichild=1:numel(hchild) |
---|
| 40 | if strcmp(get(hchild(ichild),'Visible'),'on') |
---|
| 41 | object_type=get(hchild(ichild),'Type'); |
---|
| 42 | tag=get(hchild(ichild),'tag'); |
---|
| 43 | switch object_type |
---|
| 44 | case 'uipanel' |
---|
| 45 | eval(['struct.' tag '=read_GUI(hchild(ichild));']) |
---|
| 46 | case 'uicontrol' |
---|
| 47 | object_style=get(hchild(ichild),'Style'); |
---|
| 48 | check_input=1;%default |
---|
[379] | 49 | index=0; |
---|
[281] | 50 | switch object_style |
---|
[363] | 51 | case {'checkbox','radiobutton','togglebutton'} |
---|
[295] | 52 | input=get(hchild(ichild),'Value'); |
---|
[281] | 53 | case 'edit' |
---|
[379] | 54 | separator=regexp(tag,'^num_','once');%look for the prefix 'num_' |
---|
[281] | 55 | if isempty(separator) |
---|
[323] | 56 | input=get(hchild(ichild),'String'); |
---|
[379] | 57 | else %transform into numeric if the edit box begins by the prefix 'num_' |
---|
| 58 | input=str2double(get(hchild(ichild),'String')); |
---|
[363] | 59 | tag=regexprep(tag,'^num_',''); |
---|
[379] | 60 | % detect tag name ending by an index: then interpret the input as array(index) |
---|
| 61 | r=regexp(tag,'_(?<index>\d+)$','names');% detect tag name ending by an index |
---|
| 62 | if ~isempty(r) |
---|
| 63 | tag=regexprep(tag,['_' r.index '$'],''); |
---|
| 64 | index=str2double(r.index); |
---|
| 65 | end |
---|
[363] | 66 | %deal with undefined input: retrieve the default value stored as UserData |
---|
| 67 | if isnan(input) |
---|
| 68 | input=get(hchild(ichild),'UserData'); |
---|
| 69 | set(hchild(ichild),'String',num2str(input)) |
---|
[281] | 70 | end |
---|
[363] | 71 | end |
---|
[987] | 72 | case {'listbox','popupmenu'} |
---|
[1082] | 73 | listinput=get(hchild(ichild),'String'); |
---|
| 74 | value=get(hchild(ichild),'Value'); |
---|
[323] | 75 | if ~isempty(listinput) |
---|
[427] | 76 | if numel(value)==1% single selection |
---|
[516] | 77 | if ischar(listinput) |
---|
| 78 | input=listinput; |
---|
| 79 | else |
---|
[517] | 80 | input=listinput{value}; |
---|
[516] | 81 | end |
---|
[427] | 82 | else % multiple selection |
---|
[517] | 83 | input=listinput(value); |
---|
[427] | 84 | end |
---|
[363] | 85 | else |
---|
| 86 | check_input=0; |
---|
[323] | 87 | end |
---|
[379] | 88 | separator=regexp(tag,'^num_','once'); |
---|
[363] | 89 | if ~isempty(separator) |
---|
[309] | 90 | input=str2double(input);% transform to numerical values if the uicontrol tag begins with 'num_' |
---|
[363] | 91 | tag=regexprep(tag,'^num_',''); |
---|
[309] | 92 | end |
---|
[281] | 93 | otherwise |
---|
| 94 | check_input=0; |
---|
| 95 | end |
---|
[1064] | 96 | if check_input && ~isempty(tag)% |
---|
[379] | 97 | if index==0 |
---|
| 98 | struct.(tag)=input; |
---|
[395] | 99 | elseif ~isempty(input) |
---|
[379] | 100 | struct.(tag)(index)=input; |
---|
| 101 | end |
---|
[281] | 102 | end |
---|
[350] | 103 | case 'uitable' |
---|
[363] | 104 | struct.(tag)=get(hchild(ichild),'Data'); |
---|
[281] | 105 | end |
---|
| 106 | end |
---|
| 107 | end |
---|
[591] | 108 | % read UserData if relevant |
---|
| 109 | UserData=get(handle,'UserData'); |
---|
| 110 | if isstruct(UserData) |
---|
| 111 | List=fields(UserData); |
---|
| 112 | for ilist=1:numel(List) |
---|
[711] | 113 | if isstruct(UserData.(List{ilist}))% look for edit box with the tag UserData.(List{ilist}) |
---|
[591] | 114 | heditbox=findobj(handle,'Tag',List{ilist},'Style','edit','Visible','on'); |
---|
| 115 | if isequal(numel(heditbox),1) |
---|
| 116 | struct.(List{ilist})=UserData.(List{ilist}); |
---|
[711] | 117 | else% look for pushbutton with the tag UserData.(List{ilist}) |
---|
| 118 | hpushbutton=findobj(handle,'Tag',List{ilist},'Style','pushbutton','Visible','on'); |
---|
| 119 | if isequal(numel(hpushbutton),1) |
---|
| 120 | struct.(List{ilist})=UserData.(List{ilist}); |
---|
| 121 | end |
---|
[591] | 122 | end |
---|
| 123 | end |
---|
| 124 | end |
---|
| 125 | end |
---|