source: trunk/src/fill_GUI.m @ 1119

Last change on this file since 1119 was 1107, checked in by g7moreau, 3 years ago

Update Copyright to 2022

File size: 6.0 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)
[811]4% OUTPUT:
[497]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
[809]12
13%=======================================================================
[1107]14% Copyright 2008-2022, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[809]15%   http://www.legi.grenoble-inp.fr
16%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
[497]17%
[809]18%     This file is part of the toolbox UVMAT.
19%
20%     UVMAT is free software; you can redistribute it and/or modify
21%     it under the terms of the GNU General Public License as published
22%     by the Free Software Foundation; either version 2 of the license,
23%     or (at your option) any later version.
24%
25%     UVMAT is distributed in the hope that it will be useful,
26%     but WITHOUT ANY WARRANTY; without even the implied warranty of
27%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28%     GNU General Public License (see LICENSE.txt) for more details.
29%=======================================================================
30
[591]31function errormsg=fill_GUI(Param,GUI_handle)
[360]32%------------------------------------------------------------------------
33errormsg='';
[664]34if ~isstruct(Param)
35    errormsg='first input parmaeter of fill_GUI must be a structure';
36    return
37end
[671]38children=get(GUI_handle,'children');%handles of the children of the input GUI with handle 'GUI_handle'
[664]39handles=[];
40for ichild=1:numel(children)
[671]41    if ~isempty(get(children(ichild),'tag'))
[664]42    handles.(get(children(ichild),'tag'))=children(ichild);
[671]43    end
[664]44end
[461]45fields=fieldnames(Param);%list of fields in Param
[667]46
47%--------------------------------------------------------------------------------------
48%----------------- loop on the elements of the input structure Param ------------------
49%--------------------------------------------------------------------------------------
[360]50for ifield=1:numel(fields)
[500]51    if isstruct(Param.(fields{ifield}))% case of a sub-structure
[710]52    %% case of a sub-structure
53        % if a panel in the GUI has the tag fields{ifield}, fill it with the sub-structure content
[360]54        if isfield(handles,fields{ifield})
[361]55            set(handles.(fields{ifield}),'Visible','on')
[667]56            errormsg=fill_GUI(Param.(fields{ifield}),handles.(fields{ifield}));% recursively apply the function to the substructure
[360]57        end
58    else
[667]59    %% case of an element
[361]60        hh=[];
[497]61        input_data=Param.(fields{ifield});
[379]62        check_done=0;
[361]63        if isfield(handles,fields{ifield})
[667]64        % a GUI element has a tag name equal to the key name in the element of Param
[497]65            hh=handles.(fields{ifield});
[361]66            if strcmp(get(hh,'Type'),'uitable')
[667]67            % case of a table
[379]68                set(hh,'Visible','on')
[427]69                if ischar(input_data)
[460]70                    input_data={input_data};% transform string to a single cell if needed
[427]71                end
[363]72                set(hh,'Data',input_data)
[379]73                check_done=1;
[360]74            end
[606]75        elseif isnumeric(input_data)
[667]76        % for numeric input element, look for a GUI element with the same tag name preceded by 'num_'
77            if numel(input_data)>1 % deals with array displayed in multiple boxes labeled by an index
[500]78                for ibox=1:numel(input_data)
79                    if isfield(handles,['num_' fields{ifield} '_' num2str(ibox)])
80                        hh(ibox)=handles.(['num_' fields{ifield} '_' num2str(ibox)]);
81                    end
82                end
83            else % single box (usual case)
[606]84                if isfield(handles,['num_' fields{ifield}])
85                    hh=handles.(['num_' fields{ifield}]);
86                end
[500]87            end
[361]88        end
[500]89        for ibox=1:numel(hh)
[667]90        % finalise the update of GUI uicontrol filled by the input element
[606]91            if ~isempty(hh(ibox))&& ~check_done
[667]92                set(hh(ibox),'Visible','on')% make the filled GUI element visible
[607]93                if isfield(get(hh(ibox)),'Style')
[606]94                    switch get(hh(ibox),'Style')
95                        case {'checkbox','radiobutton','togglebutton'}
96                            if isnumeric(input_data)
97                                set(hh(ibox),'Value',input_data(ibox))
98                            end
99                        case 'edit'
100                            input_string='';
101                            if isnumeric(input_data)
102                                if numel(input_data)>0
[747]103                                    input_string=num2str(input_data(ibox),4);
[606]104                                end
105                            else
106                                input_string=input_data;
107                            end
108                            set(hh(ibox),'String',input_string)
[987]109                        case {'listbox','popupmenu'}
[606]110                            if isnumeric(input_data)
[747]111                                input_data=num2str(input_data,4);
[606]112                            end
113                            menu=get(hh(ibox),'String');
114                            if ischar(input_data)
115                                input_data={input_data};
116                            end
117                            values=zeros(size(input_data));
118                            for idata=1:numel(input_data)
119                                iline=find(strcmp(input_data{idata},menu));
120                                if isempty(iline)
121                                    values(idata)=1;
122                                    menu=[input_data(idata);menu];
123                                else
124                                    values(idata)=iline(1);
125                                end
126                            end
127                            set(hh(ibox),'String',menu)
128                            set(hh(ibox),'Value',values)
[361]129                    end
[606]130                end
[360]131            end
132        end
133    end
134end
Note: See TracBrowser for help on using the repository browser.