Akshay
Akshay

Reputation: 2763

Setting handles from one GUI to another GUI - Matlab

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.

test2.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);

test1.m

function varargout = test1(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @test1_OpeningFcn, ...
                   'gui_OutputFcn',  @test1_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 test1_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;

guidata(hObject, handles);


function varargout = test1_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;

note that The GUI was developed in Matlab GUIDE.

can anyone advice me on how to do that?

Upvotes: 1

Views: 922

Answers (1)

scmg
scmg

Reputation: 1894

Use GUIDE's Inspector to set Tag to your test1, i.e my_test_1.

In your test2, find the object with such Tag before use it:

function test2_button_Callback(hObject, eventdata, handles)
obj = findall(0, 'Type', 'figure', 'Tag', 'my_test_1');
my_text = findobj(obj, 'Tag', 'test1_text');
str = sprintf('hello');
set(my_text,'String',str);

By the way, you must assure that your test1 has an object named test1_text.

Upvotes: 2

Related Questions