calling one gui from another.

hello, let's say i have two guis the first (named plotfct) contains only a pushbutton (tag: plota1) and the other (plaxes1) contains only axes (tag: axes1). what i want to do is to click on the button in the first and get the plot in the second. what i have is the next:
function plota1_Callback(hObject, eventdata, handles)
% hObject handle to plota1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x=0:0.1:5*pi;
y=2*sin(x);
plot(handles.plaxes1,x,y);
any help would be appreciated. thanks in advance.

Respuestas (3)

Image Analyst
Image Analyst el 3 de Nov. de 2014

0 votos

It would be so much easier if they were in the same GUI. Any reason why you're not doing that?

2 comentarios

Physiker192
Physiker192 el 3 de Nov. de 2014
Yeah i know. But it's my design i would like to present i would like to present it this way.
Image Analyst
Image Analyst el 3 de Nov. de 2014
You can use assignin() and evalin() to put/get variables from the base workspace. Did you read the link?

Iniciar sesión para comentar.

Julia
Julia el 3 de Nov. de 2014

0 votos

Hi,
I used something like this in the callback function in the first gui:
run GUI2;
handles.GUI2 = GUI2(handles.GUI1);
guidata(handles.GUI1,handles);
handlesVariableName = guidata(handles.GUI2);
set(handlesVariableName.pop_boom,'String',chosen); % pop_boom is the name of a drop down list in GUI2, chosen is the value I want to transfer
And in the opening function of the second gui:
try
handles.GUI1 = varargin{1};
% Choose default command line output for GUI2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
catch
end

5 comentarios

Physiker192
Physiker192 el 3 de Nov. de 2014
heyy thank you, what it did is opening the second gui but it didn't plot the function. i got an error message: ??? Reference to non-existent field 'output'.
Error in ==> plaxes1>plaxes1_OutputFcn at 78 varargout{1} = handles.output;
Error in ==> gui_mainfcn at 265 feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in ==> plaxes1 at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> run at 74 evalin('caller',[script ';']);
Error in ==> plotfct>plota1_Callback at 83 run plaxes1;
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> plotfct at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)plotfct('plota1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
could you help me more please?
Julia
Julia el 3 de Nov. de 2014
Editada: Julia el 3 de Nov. de 2014
Oh sorry! I just saw that I forgot to poste a piece of code for the second gui. This function is right after the opening function:
function varargout = GUI2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
try
varargout{1} = handles.output;
catch
end
Physiker192
Physiker192 el 3 de Nov. de 2014
still doesn't work for me. aren't there an easier way to do this. I am a bit new to GUI so if you can try my problem i would appreciate that. Thanks anyway :D
Julia
Julia el 3 de Nov. de 2014
I am not that experienced in using guis either. I just found some pieces of code and tried until it worked for me. Perhaps this is worth another try.
In:
try
handles.GUI1 = varargin{1};
% Choose default command line output for GUI2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
catch
end
GUI1 has to be the name of your first gui as you find it in the comments (generated by Matlab). Its all in uppercase letters.
Adam
Adam el 3 de Nov. de 2014
Getting GUIs to talk to each other is not particularly basic in Matlab unfortunately so it doesn't generally fall under the heading of "easy"!

Iniciar sesión para comentar.

Geoff Hayes
Geoff Hayes el 3 de Nov. de 2014

0 votos

Another possibility is to do something similar to http://www.mathworks.com/matlabcentral/answers/146215-pass-data-between-gui-s. Assuming that you are using GUIDE, you will need to set the second GUI's HandleVisibility property to on (use the GUIDE property inspector to do this) so that it can be visible to the first GUI. Now assuming that your second GUI is named (or tagged) as plaxes1, then, in your plota1_Callback do
function plota1_Callback(hObject, eventdata, handles)
x=0:0.1:5*pi;
y=2*sin(x);
h = findobj('Tag','plaxes1');
% if exists (not empty)
if ~isempty(h)
% get handles and other user-defined data associated to the other GUI
g2Handles = guidata(h);
% plot the curve
plot(g2Handles.axes1,x,y);
end
In the above, since the handle visibility of your second GUI is on, we can use findobj to look for the object whose tag is plaxes1. Here, we are assuming that the tag (another property of the GUI) has its value set to plaxes1. If you are using something else, then just insert that into the line of code. If findobj returns a (non-empty) handle, then we use guidata to get the handles (and user-defined data) of the second GUI. We can then use this structure to access the axes2 handle so that we can plot the data on it.
See attached code for an example.

Categorías

Más información sobre Interactive Control and Callbacks en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Nov. de 2014

Respondida:

el 3 de Nov. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by