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
Line 
1%'fill_GUI': fill a GUI with a set of parameters from a Matlab structure
2% -----------------------------------------------------------------------
3% function errormsg=fill_GUI(Param,GUI_handle)
4% OUPUT:
5% errormsg: error message, ='' by default
6%
7% INPUT:
8% Param: matlab structure containing the information to display in the GUI
9% GUI_handle: handle of the GUI to be filled
10%
11% see also the reverse function read_GUI.m
12%
13function errormsg=fill_GUI(Param,GUI_handle)
14%------------------------------------------------------------------------
15errormsg='';
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
25UserData=get(GUI_handle,'UserData');
26fields=fieldnames(Param);%list of fields in Param
27% loop on the elements of the input structure Param
28for ifield=1:numel(fields)
29    % case of a sub-structure --> fill a panel
30    if isstruct(Param.(fields{ifield}))% case of a sub-structure
31        if isfield(handles,fields{ifield})
32            set(handles.(fields{ifield}),'Visible','on')
33            errormsg=fill_GUI(Param.(fields{ifield}),handles.(fields{ifield}));% apply the function to the substructure
34        elseif isfield(UserData,fields{ifield})&& isfield(handles,fields{ifield})&&isfield(Param.(fields{ifield}),'Name')
35            UserData.(fields{ifield})=Param.(fields{ifield});
36            set(handles.(fields{ifield}),'String',Param.(fields{ifield}).Name)
37        end
38        % case of an element
39    else
40        hh=[];
41        input_data=Param.(fields{ifield});
42        check_done=0;
43        if isfield(handles,fields{ifield})
44            hh=handles.(fields{ifield});
45            if strcmp(get(hh,'Type'),'uitable')
46                set(hh,'Visible','on')
47                if ischar(input_data)
48                    input_data={input_data};% transform string to a single cell if needed
49                end
50                set(hh,'Data',input_data)
51                check_done=1;
52            end
53        elseif isnumeric(input_data)
54            if numel(input_data)>1
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)
62                if isfield(handles,['num_' fields{ifield}])
63                    hh=handles.(['num_' fields{ifield}]);
64                end
65            end
66        end
67        for ibox=1:numel(hh)
68            if ~isempty(hh(ibox))&& ~check_done
69                set(hh(ibox),'Visible','on')
70                %             input_data
71                if isfield(get(hh(ibox)),'Style')
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)
107                    end
108                end
109            end
110        end
111    end
112end
Note: See TracBrowser for help on using the repository browser.