0

I have two GUI files, which are as follows:

gui1.m

function varargout = gui1(varargin)

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

handles.output = hObject;

guidata(hObject, handles);

function varargout = gui1_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;

function text_Callback(hObject, eventdata, handles)



function text_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function pushbutton1_Callback(hObject, eventdata, handles)

a = get(handles.text,'String');
disp(a);

This gui1.m has a edit box and a button, when ever I click on the pushbutton1 it displays the content of it. Now I am trying to do the same with a different GUI interface with the following code.

gui2.m

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

handles.output = hObject;

guidata(hObject, handles);

function varargout = gui2_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;


function pushbutton1_Callback(hObject, eventdata, handles)

gui1('pushbutton1_Callback', hObject, eventdata, handles);

so when I click on the button pushbutton1 of gui2.m I get the following error

Reference to non-existent field 'text'.

Error in gui1>pushbutton1_Callback (line 44)
a = get(handles.text,'String');

Error in gui_mainfcn (line 96)
        feval(varargin{:});

Error in gui1 (line 17)
    gui_mainfcn(gui_State, varargin{:});

Error in gui2>pushbutton1_Callback (line 32)
gui1('pushbutton1_Callback', hObject, eventdata,
handles);
Error in gui_mainfcn (line 96)
        feval(varargin{:});

Error in gui2 (line 16)
    gui_mainfcn(gui_State, varargin{:});

Error in
@(hObject,eventdata)gui2('pushbutton1_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating uicontrol Callback

Could anyone tell me what the problem is? because when i remove the set this and assign some number to variable a in gui1.m and press the button in gui2.m I am able to display the value of a.

1 Answer 1

1

You need a shared data between two GUIs: pass the data (local data of gui1) that you want to share to a shared variable (i.e MySharedData) in gui1's pushbutton1_Callback function, and just call gui2; instead of gui1('pushbutton1_Callback', hObject, eventdata, handles);. Then in gui2's pushbutton1_Callback you can retrieve the shared data MySharedData and do what you want with the data.

There are some instructions:

http://www.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html

http://www.mathworks.com/matlabcentral/answers/338-how-to-pass-data-from-one-gui-to-another

UPDATE:

More easier way but in my opinion less reliable: when you call gui1's pushbutton, assign the value to a workspace's variable (assignin('base', 'varname', value)), then in gui2's pushbutton CallbackFcn you can get the variable with val = evalin('base', 'varname').

Sign up to request clarification or add additional context in comments.

2 Comments

this is only an example, but in the actual application i have more than 100 functions. If i just call the whole .m file it wouldn't do anything.
just store all values into a struct, and either pass the whole struct as shared data between GUIs, or pass the struct to base workspace. You can also assign the gui1 handles in base workspace, and retrieve it from gui2, i also tried both ways, and both are working good :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.