source: trunk/src/read_GUI.m @ 497

Last change on this file since 497 was 497, checked in by sommeria, 12 years ago

cleaning and small bug repair.
pb of histogram for filter data solved
display of uicontrol by right mouse selection improved

File size: 4.3 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
[363]59                            input=listinput{value};
[427]60                            else % multiple selection
61                              input=listinput(value); 
62                            end
[363]63                        else
64                            check_input=0;
[323]65                        end
[379]66                        separator=regexp(tag,'^num_','once');
[363]67                        if ~isempty(separator)
[309]68                            input=str2double(input);% transform to numerical values if the uicontrol tag begins with 'num_'
[363]69                            tag=regexprep(tag,'^num_','');
[309]70                        end
[281]71                    otherwise
72                        check_input=0;
73                end
[395]74                if check_input%
[379]75                    if index==0
76                       struct.(tag)=input;
[395]77                    elseif ~isempty(input)
[379]78                       struct.(tag)(index)=input;
79                    end
[281]80                end
[350]81            case 'uitable'
[363]82                struct.(tag)=get(hchild(ichild),'Data');
[281]83        end
84    end
85end
Note: See TracBrowser for help on using the repository browser.