source: trunk/src/fill_GUI.m @ 667

Last change on this file since 667 was 667, checked in by sommeria, 11 years ago

a few bugs corrected.
multimask introduced in series
displ_uvmat transformed into disp_uvmat

File size: 5.2 KB
RevLine 
[497]1%'fill_GUI': fill a GUI with a set of parameters from a Matlab structure
[360]2% -----------------------------------------------------------------------
[591]3% function errormsg=fill_GUI(Param,GUI_handle)
[497]4% OUPUT:
5% errormsg: error message, ='' by default
6%
7% INPUT:
8% Param: matlab structure containing the information to display in the GUI
[664]9% GUI_handle: handle of the GUI to be filled
[497]10%
11% see also the reverse function read_GUI.m
12%
[591]13function errormsg=fill_GUI(Param,GUI_handle)
[360]14%------------------------------------------------------------------------
15errormsg='';
[664]16if ~isstruct(Param)
17    errormsg='first input parmaeter of fill_GUI must be a structure';
18    return
19end
20children=get(GUI_handle,'children');
21handles=[];
22for ichild=1:numel(children)
23    handles.(get(children(ichild),'tag'))=children(ichild);
24end
[591]25UserData=get(GUI_handle,'UserData');
[461]26fields=fieldnames(Param);%list of fields in Param
[667]27
28%--------------------------------------------------------------------------------------
29%----------------- loop on the elements of the input structure Param ------------------
30%--------------------------------------------------------------------------------------
[360]31for ifield=1:numel(fields)
[500]32    if isstruct(Param.(fields{ifield}))% case of a sub-structure
[667]33    %% case of a sub-structure --> fill a panel
[360]34        if isfield(handles,fields{ifield})
[361]35            set(handles.(fields{ifield}),'Visible','on')
[667]36            errormsg=fill_GUI(Param.(fields{ifield}),handles.(fields{ifield}));% recursively apply the function to the substructure
[591]37        elseif isfield(UserData,fields{ifield})&& isfield(handles,fields{ifield})&&isfield(Param.(fields{ifield}),'Name')
38            UserData.(fields{ifield})=Param.(fields{ifield});
[606]39            set(handles.(fields{ifield}),'String',Param.(fields{ifield}).Name)
[360]40        end
41    else
[667]42    %% case of an element
[361]43        hh=[];
[497]44        input_data=Param.(fields{ifield});
[379]45        check_done=0;
[361]46        if isfield(handles,fields{ifield})
[667]47        % a GUI element has a tag name equal to the key name in the element of Param
[497]48            hh=handles.(fields{ifield});
[361]49            if strcmp(get(hh,'Type'),'uitable')
[667]50            % case of a table
[379]51                set(hh,'Visible','on')
[427]52                if ischar(input_data)
[460]53                    input_data={input_data};% transform string to a single cell if needed
[427]54                end
[363]55                set(hh,'Data',input_data)
[379]56                check_done=1;
[360]57            end
[606]58        elseif isnumeric(input_data)
[667]59        % for numeric input element, look for a GUI element with the same tag name preceded by 'num_'
60            if numel(input_data)>1 % deals with array displayed in multiple boxes labeled by an index
[500]61                for ibox=1:numel(input_data)
62                    if isfield(handles,['num_' fields{ifield} '_' num2str(ibox)])
63                        hh(ibox)=handles.(['num_' fields{ifield} '_' num2str(ibox)]);
64                    end
65                end
66            else % single box (usual case)
[606]67                if isfield(handles,['num_' fields{ifield}])
68                    hh=handles.(['num_' fields{ifield}]);
69                end
[500]70            end
[361]71        end
[500]72        for ibox=1:numel(hh)
[667]73        % finalise the update of GUI uicontrol filled by the input element
[606]74            if ~isempty(hh(ibox))&& ~check_done
[667]75                set(hh(ibox),'Visible','on')% make the filled GUI element visible
[607]76                if isfield(get(hh(ibox)),'Style')
[606]77                    switch get(hh(ibox),'Style')
78                        case {'checkbox','radiobutton','togglebutton'}
79                            if isnumeric(input_data)
80                                set(hh(ibox),'Value',input_data(ibox))
81                            end
82                        case 'edit'
83                            input_string='';
84                            if isnumeric(input_data)
85                                if numel(input_data)>0
86                                    input_string=num2str(input_data(ibox));
87                                end
88                            else
89                                input_string=input_data;
90                            end
91                            set(hh(ibox),'String',input_string)
92                        case{'listbox','popupmenu'}
93                            if isnumeric(input_data)
94                                input_data=num2str(input_data);
95                            end
96                            menu=get(hh(ibox),'String');
97                            if ischar(input_data)
98                                input_data={input_data};
99                            end
100                            values=zeros(size(input_data));
101                            for idata=1:numel(input_data)
102                                iline=find(strcmp(input_data{idata},menu));
103                                if isempty(iline)
104                                    values(idata)=1;
105                                    menu=[input_data(idata);menu];
106                                else
107                                    values(idata)=iline(1);
108                                end
109                            end
110                            set(hh(ibox),'String',menu)
111                            set(hh(ibox),'Value',values)
[361]112                    end
[606]113                end
[360]114            end
115        end
116    end
117end
Note: See TracBrowser for help on using the repository browser.