Set workspace data to sub GUI by push button on main GUI
1 次查看(过去 30 天)
显示 更早的评论
Hello there,
I am currently working on a simulation program, which consists of two GUI's. I am having difficulties setting an edit text box with a value in the sub GUI by using a push button on the main GUI.
The main GUI has an "OPEN" button, which opens a .mat-file consisting a list of variables and sets these variables to the workspace. Some variables have to be set to the main GUI and some variables should be set to the sub GUI. I can open the sub GUI with a button "SUBGUI" on the main GUI.
Example
I can open the sub GUI with a button "SUBGUI" on the main GUI, and the main GUI has an "OPEN" button, which opens a .mat-file consisting variables "a" and "b".The two variables "a" and "b" are opened from the .mat-file and added to the workspace. "a" should be set to an edit text box in the main GUI and "b" should be set to an edit texg box in the sub GUI. Currently I am using this code.
a = 2 b = 3
function Open_Callback(hObject, eventdata, handles)
%Open .mat-file with values a and b
[FileName,PathName]=uigetfile('*.mat','Select .mat file to open');
if isequal (FileName,0)
msgbox('Cancelled')
else
evalin('base',['load ' FileName]);
end
%setting handles edit text box a to main GUI
set(handles.a, 'String', evalin('base','a'));
%setting handles edit text box b to sub GUI
b=evalin('base','b');
set(handles.b,'String',b);
This works for the main GUI, but not for the sub GUI. There are two problems:
1) When the variables are set to the workspace and I open the Sub GUI an error is generated. However, the string of the edit text box b is set to 3.
Reference to non-existent field 'b'.
Error in VibrationModel>Open_Callback (line 338)
set(handles.b,'String',b);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in VibrationModel (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)VibrationModel('Open_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
2) If I haven't opened a .mat-file and the workspace is empty, the Sub GUI cannot be opened. Matlab generates an error for not having a variable b in the workspace.
Error using evalin
Undefined function or variable 'b'.
Error in SUBGUI>SUBGUI_OpeningFcn (line 58)
set(handles.b,'String',evalin('base','b'));
Error in gui_mainfcn (line 220)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in SUBGUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in VibrationModel>SUBGUI_Callback (line 83)
SUBGUI;
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in VibrationModel (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)VibrationModel('SUBGUI_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I hope anyone can help me solving these problems.
2 个评论
Adam
2014-12-8
Personally I would use a class derived from handle that I pass to the subgui when I create it from the main GUI.
The class would contain properties for whatever needs to be passed to the subgui and the subgui would attach a listener either to the individual properties or to an even that gets triggered when something happens on the class (e.g. all properties updated).
Having GUIs fishing around into each others' handles structures via findall always makes me shudder from a software engineering perspective.
Looks like orion has provided a highly detailed answer below though so I'll not provide a full answer via this method.
is worth looking into though as it allows for neater coding and UIs controlling only their own components rather than a system where anything can change anything and bugs could sprout up from anywhere!
采纳的回答
Orion
2014-12-8
编辑:Orion
2014-12-8
See the attached files.
Run the Main gui.
The pushbutton will open the subgui, load the values a and b stored in the mat file and will display a in the main and b in the sub.
Just adapt this to your code and it should work.
3 个评论
Orion
2014-12-8
Pleasure.
Other advice : don't name your objects with names like a, b,..
Put some real variable names such as : EditBox_A, EditBox_B
your code will be more readable and robust.
更多回答(2 个)
Orion
2014-12-5
Hi,
you are not using the right structure of handles.
the box a is in the maingui.
the box b is in the subgui.
In your code
function Open_Callback(hObject, eventdata, handles)
%Open .mat-file with values a and b
[FileName,PathName]=uigetfile('*.mat','Select .mat file to open');
if isequal (FileName,0)
msgbox('Cancelled')
else
evalin('base',['load ' FileName]);
end
%setting handles edit text box a to main GUI
set(handles.a, 'String', evalin('base','a'));
%setting handles edit text box b to sub GUI
b=evalin('base','b');
set(handles.b,'String',b);
handles is the structure of handles of your maingui components only .
so you can't access the b box with this structure, you need to get the handle structure of the subgui first.
something like
function Open_Callback(hObject, eventdata, handles)
%Open .mat-file with values a and b
[FileName,PathName]=uigetfile('*.mat','Select .mat file to open');
if isequal (FileName,0)
msgbox('Cancelled')
else
evalin('base',['load ' FileName]);
end
%setting handles edit text box a to main GUI
set(handles.a, 'String', evalin('base','a'));
%setting handles edit text box b to sub GUI
b=evalin('base','b');
% getting the handles of subgui
subhandles = guidata(findall(0,'Tag','TagOfYourSubGui'))
% now, modify the b box
set(subhandles.b,'String',b);
3 个评论
Orion
2014-12-5
you changed the line I wrote.
Keep the 'TAG' property in :
findall(0,'tag','SUBGUI')
this command line will return the handle of your subfigure (which has SUBGUI as a tag).
And Then, you can use guidata on it.
Just to be sure : 'SUBGUI' is the tag of your figure ? otherwise just put the tag of your subgui
subhandles = guidata(findall(0,'Tag','TagOfYourSubGui'))
SANDEEP C S
2017-3-26
Error in cfg_ui>cfg_ui_OpeningFcn (line 1001) cfg_onscreen(hObject);
Error in gui_mainfcn (line 220) feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in cfg_ui (line 51) [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
Error in spm_jobman (line 162) f = cfg_ui('Visible','off'); % Create invisible batch ui
Error in spm (line 353) spm_jobman('initcfg');
Error while evaluating uicontrol Callback
>>
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Gain Scheduling 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!