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