source: trunk/src/fill_GUI.m @ 603

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

bugs corrected, improvement of civ_input GUI

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