source: trunk/src/read_GUI.m @ 500

Last change on this file since 500 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
Line 
1% 'read_GUI':read a GUI and provide the data as a Matlab structure
2%----------------------------------------------------------------------
3% function struct=read_GUI(handle)
4%
5% OUTPUT:
6% struct: matlab structure containing the information displayed in the GUI
7% The content of a panel with tag 'tag' is displayed as a substructure struct.(tag) (recursive use of read_GUI)
8% Output of a GUI element with tag 'tag':
9%     -case 'checkbox','radiobutton','togglebutton': struct.(tag)=value
10%     -case'edit': struct.(tag)=string, 
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.
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
15%
16function struct=read_GUI(handle)
17%------------------------------------------------------------------------
18struct=[];%default
19hchild=get(handle,'children');
20hchild=flipdim(hchild,1);% reverse the order to respect the tab order in the GUI
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
31                index=0;
32                switch object_style
33                    case {'checkbox','radiobutton','togglebutton'}
34                        input=get(hchild(ichild),'Value');
35                    case 'edit'
36                        separator=regexp(tag,'^num_','once');%look for the prefix 'num_'
37                        if isempty(separator)
38                            input=get(hchild(ichild),'String');
39                        else  %transform into numeric if the edit box begins by the prefix 'num_'                                           
40                            input=str2double(get(hchild(ichild),'String'));                       
41                            tag=regexprep(tag,'^num_','');
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
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))
52                            end
53                        end                       
54                    case{'listbox','popupmenu'}
55                        listinput=get(hchild(ichild),'String');
56                        value=get(hchild(ichild),'Value');
57                        if ~isempty(listinput)
58                            if numel(value)==1% single selection
59                            input=listinput{value};
60                            else % multiple selection
61                              input=listinput(value); 
62                            end
63                        else
64                            check_input=0;
65                        end
66                        separator=regexp(tag,'^num_','once');
67                        if ~isempty(separator)
68                            input=str2double(input);% transform to numerical values if the uicontrol tag begins with 'num_'
69                            tag=regexprep(tag,'^num_','');
70                        end
71                    otherwise
72                        check_input=0;
73                end
74                if check_input%
75                    if index==0
76                       struct.(tag)=input;
77                    elseif ~isempty(input)
78                       struct.(tag)(index)=input;
79                    end
80                end
81            case 'uitable'
82                struct.(tag)=get(hchild(ichild),'Data');
83        end
84    end
85end
Note: See TracBrowser for help on using the repository browser.