[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 | %======================================================================= |
---|
[1126] | 14 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
[809] | 15 | % http://www.legi.grenoble-inp.fr |
---|
[1127] | 16 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.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] | 31 | function errormsg=fill_GUI(Param,GUI_handle) |
---|
[360] | 32 | %------------------------------------------------------------------------ |
---|
| 33 | errormsg=''; |
---|
[664] | 34 | if ~isstruct(Param) |
---|
| 35 | errormsg='first input parmaeter of fill_GUI must be a structure'; |
---|
| 36 | return |
---|
| 37 | end |
---|
[671] | 38 | children=get(GUI_handle,'children');%handles of the children of the input GUI with handle 'GUI_handle' |
---|
[664] | 39 | handles=[]; |
---|
| 40 | for 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] | 44 | end |
---|
[461] | 45 | fields=fieldnames(Param);%list of fields in Param |
---|
[667] | 46 | |
---|
| 47 | %-------------------------------------------------------------------------------------- |
---|
| 48 | %----------------- loop on the elements of the input structure Param ------------------ |
---|
| 49 | %-------------------------------------------------------------------------------------- |
---|
[360] | 50 | for 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}); |
---|
[1123] | 62 | if isfield(Param,'Type') && isa(Param.Type,'matlab.graphics.primitive.Rectangle') |
---|
| 63 | input_data='rectangle'; |
---|
| 64 | end |
---|
[379] | 65 | check_done=0; |
---|
[361] | 66 | if isfield(handles,fields{ifield}) |
---|
[667] | 67 | % a GUI element has a tag name equal to the key name in the element of Param |
---|
[497] | 68 | hh=handles.(fields{ifield}); |
---|
[361] | 69 | if strcmp(get(hh,'Type'),'uitable') |
---|
[667] | 70 | % case of a table |
---|
[379] | 71 | set(hh,'Visible','on') |
---|
[427] | 72 | if ischar(input_data) |
---|
[460] | 73 | input_data={input_data};% transform string to a single cell if needed |
---|
[427] | 74 | end |
---|
[363] | 75 | set(hh,'Data',input_data) |
---|
[379] | 76 | check_done=1; |
---|
[360] | 77 | end |
---|
[606] | 78 | elseif isnumeric(input_data) |
---|
[667] | 79 | % for numeric input element, look for a GUI element with the same tag name preceded by 'num_' |
---|
| 80 | if numel(input_data)>1 % deals with array displayed in multiple boxes labeled by an index |
---|
[500] | 81 | for ibox=1:numel(input_data) |
---|
| 82 | if isfield(handles,['num_' fields{ifield} '_' num2str(ibox)]) |
---|
| 83 | hh(ibox)=handles.(['num_' fields{ifield} '_' num2str(ibox)]); |
---|
| 84 | end |
---|
| 85 | end |
---|
| 86 | else % single box (usual case) |
---|
[606] | 87 | if isfield(handles,['num_' fields{ifield}]) |
---|
| 88 | hh=handles.(['num_' fields{ifield}]); |
---|
| 89 | end |
---|
[500] | 90 | end |
---|
[361] | 91 | end |
---|
[500] | 92 | for ibox=1:numel(hh) |
---|
[667] | 93 | % finalise the update of GUI uicontrol filled by the input element |
---|
[606] | 94 | if ~isempty(hh(ibox))&& ~check_done |
---|
[667] | 95 | set(hh(ibox),'Visible','on')% make the filled GUI element visible |
---|
[607] | 96 | if isfield(get(hh(ibox)),'Style') |
---|
[606] | 97 | switch get(hh(ibox),'Style') |
---|
| 98 | case {'checkbox','radiobutton','togglebutton'} |
---|
| 99 | if isnumeric(input_data) |
---|
| 100 | set(hh(ibox),'Value',input_data(ibox)) |
---|
| 101 | end |
---|
| 102 | case 'edit' |
---|
| 103 | input_string=''; |
---|
| 104 | if isnumeric(input_data) |
---|
| 105 | if numel(input_data)>0 |
---|
[747] | 106 | input_string=num2str(input_data(ibox),4); |
---|
[606] | 107 | end |
---|
[1122] | 108 | elseif ischar(input_data) |
---|
[606] | 109 | input_string=input_data; |
---|
| 110 | end |
---|
| 111 | set(hh(ibox),'String',input_string) |
---|
[987] | 112 | case {'listbox','popupmenu'} |
---|
[606] | 113 | if isnumeric(input_data) |
---|
[747] | 114 | input_data=num2str(input_data,4); |
---|
[606] | 115 | end |
---|
| 116 | menu=get(hh(ibox),'String'); |
---|
| 117 | if ischar(input_data) |
---|
| 118 | input_data={input_data}; |
---|
| 119 | end |
---|
| 120 | values=zeros(size(input_data)); |
---|
| 121 | for idata=1:numel(input_data) |
---|
| 122 | iline=find(strcmp(input_data{idata},menu)); |
---|
| 123 | if isempty(iline) |
---|
| 124 | values(idata)=1; |
---|
| 125 | menu=[input_data(idata);menu]; |
---|
| 126 | else |
---|
| 127 | values(idata)=iline(1); |
---|
| 128 | end |
---|
| 129 | end |
---|
| 130 | set(hh(ibox),'String',menu) |
---|
| 131 | set(hh(ibox),'Value',values) |
---|
[361] | 132 | end |
---|
[606] | 133 | end |
---|
[360] | 134 | end |
---|
| 135 | end |
---|
| 136 | end |
---|
| 137 | end |
---|