Setting handles from one GUI to another GUI - Matlab

3 次查看(过去 30 天)
I am new to Matlab, excuse my amateur coding. I am trying to pass handles from one GUI to another GUI which are two independent GUI's.
for example, I created two GUI's test1.m and test2.m, in which test2.m calls test1.m in the opening function. so here I am trying to set the text on the test1.m using its handles. But I get an error Reference to non-existent field test1_text. I have even tried sending the handles of test2.m to test1.m by doing test1(handles) in the opening function but still I get the same error.
.m sets the text in the second GUI:
function varargout = test2(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @test2_OpeningFcn, ...
'gui_OutputFcn', @test2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function test2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
test1
guidata(hObject, handles);
function varargout = test2_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function test2_button_Callback(hObject, eventdata, handles)
str = sprintf('hello');
set(handles.test1_text,'String',str);
note that The GUI was developed in Matlab GUIDE.
can anyone advice me on how to do that?

回答(1 个)

Voss
Voss 2024-1-10
Here's how you can do it. (test1 and test2 m- and fig-files are attached; contents of test2.m reproduced below.)
function varargout = test2(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @test2_OpeningFcn, ...
'gui_OutputFcn', @test2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function test2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% call test1, and store its figure in test2's handles structure:
handles.test1_figure = test1();
guidata(hObject, handles);
function varargout = test2_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function test2_button_Callback(hObject, eventdata, handles)
str = 'hello';
% get the handles structure of test1 using guidata():
test1handles = guidata(handles.test1_figure);
% update the string of the text in test1:
set(test1handles.text,'String',str);

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by