source: trunk/src/fill_GUI.m @ 598

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

various bugs repaired . civ_series further developed

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% handles: Matlab structure containing the handles of the GUI elements
10%
11% see also the reverse function read_GUI.m
12%
13function errormsg=fill_GUI(Param,GUI_handle)
14%------------------------------------------------------------------------
15errormsg='';
16%handles=guidata(GUI_handle);
17            children=get(GUI_handle,'children');
18            for ichild=1:numel(children)
19                handles.(get(children(ichild),'tag'))=children(ichild);
20            end
21UserData=get(GUI_handle,'UserData');
22fields=fieldnames(Param);%list of fields in Param
23% loop on the elements of the input structure Param
24for ifield=1:numel(fields)
25    % case of a sub-structure --> fill a panel
26    if isstruct(Param.(fields{ifield}))% case of a sub-structure
27        if isfield(handles,fields{ifield})
28            set(handles.(fields{ifield}),'Visible','on')
29%             children=get(handles.(fields{ifield}),'children');
30%             for ichild=1:numel(children)
31%                 hchild.(get(children(ichild),'tag'))=children(ichild);
32%             end
33         %   errormsg=fill_GUI(Param.(fields{ifield}),hchild);% apply the function to the substructure
34         errormsg=fill_GUI(Param.(fields{ifield}),handles.(fields{ifield}));% apply the function to the substructure
35           % if the input sub-structure fits with a tag name of the GUI and a
36         % substructure of UserData
37        elseif isfield(UserData,fields{ifield})&& isfield(handles,fields{ifield})&&isfield(Param.(fields{ifield}),'Name')
38            UserData.(fields{ifield})=Param.(fields{ifield});
39            set(handles.(fields{ifield}),'String',Param.(fields{ifield}).Name)
40        end
41    % case of an element
42    else
43        hh=[];
44        input_data=Param.(fields{ifield});
45        check_done=0;
46        if isfield(handles,fields{ifield})
47            hh=handles.(fields{ifield});
48            if strcmp(get(hh,'Type'),'uitable')
49                set(hh,'Visible','on')
50                if ischar(input_data)
51                    input_data={input_data};% transform string to a single cell if needed
52                end
53                set(hh,'Data',input_data)
54                check_done=1;
55            end
56        elseif isnumeric(input_data)
57            if numel(input_data)>1
58                %deals with array displayed in multiple boxes labeled by an index
59                for ibox=1:numel(input_data)
60                    if isfield(handles,['num_' fields{ifield} '_' num2str(ibox)])
61                        hh(ibox)=handles.(['num_' fields{ifield} '_' num2str(ibox)]);
62                    end
63                end
64            else % single box (usual case)
65               if isfield(handles,['num_' fields{ifield}])
66                   hh=handles.(['num_' fields{ifield}]);
67               end
68            end
69        end
70        for ibox=1:numel(hh)
71        if ~isempty(hh(ibox))&& ~check_done
72            set(hh(ibox),'Visible','on')
73%             input_data
74            switch get(hh(ibox),'Style')
75                case {'checkbox','radiobutton','togglebutton'}
76                    if isnumeric(input_data)
77                        set(hh(ibox),'Value',input_data(ibox))
78                    end
79                case 'edit'
80                    input_string='';
81                    if isnumeric(input_data)
82                        if numel(input_data)>0
83                        input_string=num2str(input_data(ibox));
84                        end
85                    else
86                        input_string=input_data;
87                    end
88                    set(hh(ibox),'String',input_string)
89                case{'listbox','popupmenu'}
90                    if isnumeric(input_data)
91                        input_data=num2str(input_data);
92                    end
93                    menu=get(hh(ibox),'String');
94                    if ischar(input_data)
95                        input_data={input_data};
96                    end
97                    values=zeros(size(input_data));
98                    for idata=1:numel(input_data)
99                        iline=find(strcmp(input_data{idata},menu));
100                        if isempty(iline)
101                            values(idata)=1;
102                            menu=[input_data(idata);menu];
103                        else
104                            values(idata)=iline(1);
105                        end
106                    end
107                    set(hh(ibox),'String',menu)
108                    set(hh(ibox),'Value',values)
109            end
110        end
111        end
112    end
113end
114 
Note: See TracBrowser for help on using the repository browser.