source: trunk/src/read_GUI.m @ 380

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

several bugs corrected
set_object.fig rationalized so that read_set_object is replaced by the rgeneral fct read_GUI.

File size: 3.3 KB
RevLine 
[309]1% -----------------------------------------------------------------------
[281]2% --- read a GUI with handle 'handle' producing a structure 'struct'
3function struct=read_GUI(handle)
[309]4%------------------------------------------------------------------------
[287]5struct=[];%default
[281]6hchild=get(handle,'children');
[379]7hchild=flipdim(hchild,1);% reverse the order to respect the tab order in the GUI
[281]8for ichild=1:numel(hchild)
9    if strcmp(get(hchild(ichild),'Visible'),'on')
10        object_type=get(hchild(ichild),'Type');
11        tag=get(hchild(ichild),'tag');
12        switch object_type
13            case 'uipanel'
14                eval(['struct.' tag '=read_GUI(hchild(ichild));'])
15            case 'uicontrol'
16                object_style=get(hchild(ichild),'Style');
17                check_input=1;%default
[379]18                index=0;
[281]19                switch object_style
[363]20                    case {'checkbox','radiobutton','togglebutton'}
[295]21                        input=get(hchild(ichild),'Value');
[281]22                    case 'edit'
[379]23                        separator=regexp(tag,'^num_','once');%look for the prefix 'num_'
[281]24                        if isempty(separator)
[323]25                            input=get(hchild(ichild),'String');
[379]26                        else  %transform into numeric if the edit box begins by the prefix 'num_'                                           
27                            input=str2double(get(hchild(ichild),'String'));                       
[363]28                            tag=regexprep(tag,'^num_','');
[379]29                            % detect tag name ending by an index: then interpret the input as array(index)
30                            r=regexp(tag,'_(?<index>\d+)$','names');% detect tag name ending by an index
31                            if ~isempty(r)
32                                tag=regexprep(tag,['_' r.index '$'],'');
33                                index=str2double(r.index);
34                            end
[363]35                            %deal with undefined input: retrieve the default value stored as UserData
36                            if isnan(input)
37                                input=get(hchild(ichild),'UserData');
38                                set(hchild(ichild),'String',num2str(input))
[281]39                            end
[363]40                        end                       
41                    case{'listbox','popupmenu'}
[295]42                        listinput=get(hchild(ichild),'String');
43                        value=get(hchild(ichild),'Value');
[323]44                        if ~isempty(listinput)
[363]45                            input=listinput{value};
46                        else
47                            check_input=0;
[323]48                        end
[379]49                        separator=regexp(tag,'^num_','once');
[363]50                        if ~isempty(separator)
[309]51                            input=str2double(input);% transform to numerical values if the uicontrol tag begins with 'num_'
[363]52                            tag=regexprep(tag,'^num_','');
[309]53                        end
[281]54                    otherwise
55                        check_input=0;
56                end
57                if check_input
[379]58                    if index==0
59                       struct.(tag)=input;
60                    else
61                       struct.(tag)(index)=input;
62                    end
[281]63                end
[350]64            case 'uitable'
[363]65                struct.(tag)=get(hchild(ichild),'Data');
[281]66        end
67    end
68end
Note: See TracBrowser for help on using the repository browser.