source: trunk/src/fill_GUI.m @ 664

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

a few bugs corrected: import and retrival of parameters in series. mask in civ_series

File size: 4.7 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
[472]27% loop on the elements of the input structure Param
[360]28for ifield=1:numel(fields)
[500]29    % case of a sub-structure --> fill a panel
30    if isstruct(Param.(fields{ifield}))% case of a sub-structure
[360]31        if isfield(handles,fields{ifield})
[361]32            set(handles.(fields{ifield}),'Visible','on')
[606]33            errormsg=fill_GUI(Param.(fields{ifield}),handles.(fields{ifield}));% apply the function to the substructure
[591]34        elseif isfield(UserData,fields{ifield})&& isfield(handles,fields{ifield})&&isfield(Param.(fields{ifield}),'Name')
35            UserData.(fields{ifield})=Param.(fields{ifield});
[606]36            set(handles.(fields{ifield}),'String',Param.(fields{ifield}).Name)
[360]37        end
[606]38        % case of an element
[360]39    else
[361]40        hh=[];
[497]41        input_data=Param.(fields{ifield});
[379]42        check_done=0;
[361]43        if isfield(handles,fields{ifield})
[497]44            hh=handles.(fields{ifield});
[361]45            if strcmp(get(hh,'Type'),'uitable')
[379]46                set(hh,'Visible','on')
[427]47                if ischar(input_data)
[460]48                    input_data={input_data};% transform string to a single cell if needed
[427]49                end
[363]50                set(hh,'Data',input_data)
[379]51                check_done=1;
[360]52            end
[606]53        elseif isnumeric(input_data)
54            if numel(input_data)>1
[500]55                %deals with array displayed in multiple boxes labeled by an index
56                for ibox=1:numel(input_data)
57                    if isfield(handles,['num_' fields{ifield} '_' num2str(ibox)])
58                        hh(ibox)=handles.(['num_' fields{ifield} '_' num2str(ibox)]);
59                    end
60                end
61            else % single box (usual case)
[606]62                if isfield(handles,['num_' fields{ifield}])
63                    hh=handles.(['num_' fields{ifield}]);
64                end
[500]65            end
[361]66        end
[500]67        for ibox=1:numel(hh)
[606]68            if ~isempty(hh(ibox))&& ~check_done
69                set(hh(ibox),'Visible','on')
70                %             input_data
[607]71                if isfield(get(hh(ibox)),'Style')
[606]72                    switch get(hh(ibox),'Style')
73                        case {'checkbox','radiobutton','togglebutton'}
74                            if isnumeric(input_data)
75                                set(hh(ibox),'Value',input_data(ibox))
76                            end
77                        case 'edit'
78                            input_string='';
79                            if isnumeric(input_data)
80                                if numel(input_data)>0
81                                    input_string=num2str(input_data(ibox));
82                                end
83                            else
84                                input_string=input_data;
85                            end
86                            set(hh(ibox),'String',input_string)
87                        case{'listbox','popupmenu'}
88                            if isnumeric(input_data)
89                                input_data=num2str(input_data);
90                            end
91                            menu=get(hh(ibox),'String');
92                            if ischar(input_data)
93                                input_data={input_data};
94                            end
95                            values=zeros(size(input_data));
96                            for idata=1:numel(input_data)
97                                iline=find(strcmp(input_data{idata},menu));
98                                if isempty(iline)
99                                    values(idata)=1;
100                                    menu=[input_data(idata);menu];
101                                else
102                                    values(idata)=iline(1);
103                                end
104                            end
105                            set(hh(ibox),'String',menu)
106                            set(hh(ibox),'Value',values)
[361]107                    end
[606]108                end
[360]109            end
110        end
111    end
112end
Note: See TracBrowser for help on using the repository browser.