source: trunk/src/fill_GUI.m @ 738

Last change on this file since 738 was 710, checked in by sommeria, 10 years ago

various improvements.

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