source: trunk/src/read_GUI.m @ 711

Last change on this file since 711 was 711, checked in by sommeria, 10 years ago

various improvements

File size: 5.2 KB
RevLine 
[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
[395]15%
[281]16function struct=read_GUI(handle)
[309]17%------------------------------------------------------------------------
[287]18struct=[];%default
[281]19hchild=get(handle,'children');
[379]20hchild=flipdim(hchild,1);% reverse the order to respect the tab order in the GUI
[281]21for ichild=1:numel(hchild)
22    if strcmp(get(hchild(ichild),'Visible'),'on')
23        object_type=get(hchild(ichild),'Type');
24        tag=get(hchild(ichild),'tag');
25        switch object_type
26            case 'uipanel'
27                eval(['struct.' tag '=read_GUI(hchild(ichild));'])
28            case 'uicontrol'
29                object_style=get(hchild(ichild),'Style');
30                check_input=1;%default
[379]31                index=0;
[281]32                switch object_style
[363]33                    case {'checkbox','radiobutton','togglebutton'}
[295]34                        input=get(hchild(ichild),'Value');
[281]35                    case 'edit'
[379]36                        separator=regexp(tag,'^num_','once');%look for the prefix 'num_'
[281]37                        if isempty(separator)
[323]38                            input=get(hchild(ichild),'String');
[379]39                        else  %transform into numeric if the edit box begins by the prefix 'num_'                                           
40                            input=str2double(get(hchild(ichild),'String'));                       
[363]41                            tag=regexprep(tag,'^num_','');
[379]42                            % detect tag name ending by an index: then interpret the input as array(index)
43                            r=regexp(tag,'_(?<index>\d+)$','names');% detect tag name ending by an index
44                            if ~isempty(r)
45                                tag=regexprep(tag,['_' r.index '$'],'');
46                                index=str2double(r.index);
47                            end
[363]48                            %deal with undefined input: retrieve the default value stored as UserData
49                            if isnan(input)
50                                input=get(hchild(ichild),'UserData');
51                                set(hchild(ichild),'String',num2str(input))
[281]52                            end
[363]53                        end                       
54                    case{'listbox','popupmenu'}
[295]55                        listinput=get(hchild(ichild),'String');
56                        value=get(hchild(ichild),'Value');
[323]57                        if ~isempty(listinput)
[427]58                            if numel(value)==1% single selection
[516]59                                if ischar(listinput)
60                                    input=listinput;
61                                else
[517]62                                    input=listinput{value};
[516]63                                end
[427]64                            else % multiple selection
[517]65                                input=listinput(value);
[427]66                            end
[363]67                        else
68                            check_input=0;
[323]69                        end
[379]70                        separator=regexp(tag,'^num_','once');
[363]71                        if ~isempty(separator)
[309]72                            input=str2double(input);% transform to numerical values if the uicontrol tag begins with 'num_'
[363]73                            tag=regexprep(tag,'^num_','');
[309]74                        end
[281]75                    otherwise
76                        check_input=0;
77                end
[395]78                if check_input%
[379]79                    if index==0
80                       struct.(tag)=input;
[395]81                    elseif ~isempty(input)
[379]82                       struct.(tag)(index)=input;
83                    end
[281]84                end
[350]85            case 'uitable'
[363]86                struct.(tag)=get(hchild(ichild),'Data');
[281]87        end
88    end
89end
[591]90% read UserData if relevant
91UserData=get(handle,'UserData');
92if isstruct(UserData)
93    List=fields(UserData);
94    for ilist=1:numel(List)
[711]95        if isstruct(UserData.(List{ilist}))% look for edit box with the tag UserData.(List{ilist})
[591]96            heditbox=findobj(handle,'Tag',List{ilist},'Style','edit','Visible','on');
97            if isequal(numel(heditbox),1)
98                struct.(List{ilist})=UserData.(List{ilist});
[711]99            else% look for pushbutton with the tag UserData.(List{ilist})
100                hpushbutton=findobj(handle,'Tag',List{ilist},'Style','pushbutton','Visible','on');
101                if isequal(numel(hpushbutton),1)
102                    struct.(List{ilist})=UserData.(List{ilist});
103                end
[591]104            end
105        end
106    end
107end
Note: See TracBrowser for help on using the repository browser.