Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Talking to multiple GUI

Subject: Talking to multiple GUI

From: Amol

Date: 8 Feb, 2008 04:21:02

Message: 1 of 9

I am having a lot of trouble in trying to talk to multiple
GUIs. I have created fig1 and fig2 which have seperate fig
and m files. Fig1 has a Text Box and fig2 has a Button. What
I would like to do is control something in fig1 by doing
something in fig2 e.g. change the text in TextBox of fig1 by
pressing the button in fig2. Can someone please tell me how
I should go by it? I start in fig2 and then

h = fig1;

I see fig1 but I cannot control anything beyond that... Any
suggestions?

Subject: Talking to multiple GUI

From: Ashwini Deshpande

Date: 8 Feb, 2008 05:00:22

Message: 2 of 9

"Amol " <amol.borkar@gmail.com> wrote in message
<foglbe$o9o$1@fred.mathworks.com>...
> I am having a lot of trouble in trying to talk to multiple
> GUIs. I have created fig1 and fig2 which have seperate fig
> and m files. Fig1 has a Text Box and fig2 has a Button. What
> I would like to do is control something in fig1 by doing
> something in fig2 e.g. change the text in TextBox of fig1 by
> pressing the button in fig2. Can someone please tell me how
> I should go by it? I start in fig2 and then
>
> h = fig1;
>
> I see fig1 but I cannot control anything beyond that... Any
> suggestions?
Hi Amol,

You have to define the uicontrol handle as global so that
you can control it from outside the file.

for example:
Fig1 m-file:
function EditBox_CreateFcn(hObject, eventdata, handles)
global hObject_EditBox
hObject_EditBox = hObject;

function EditBox_Callback(hObject, eventdata, handles)
global EditBox
  
Fig2 m-file:
function Button_Callback(hObject, eventdata, handles)
global hObject_EditBox
set(hObject_EditBox, 'String', 'SUCCESS');

i hope this will work, if not plz let me know.

Ashwini

Subject: Talking to multiple GUI

From: Pekka

Date: 8 Feb, 2008 13:30:05

Message: 3 of 9

"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in
message <fognl6$nc1$1@fred.mathworks.com>...
> "Amol " <amol.borkar@gmail.com> wrote in message
> <foglbe$o9o$1@fred.mathworks.com>...
> > I am having a lot of trouble in trying to talk to
multiple
> > GUIs. I have created fig1 and fig2 which have seperate
fig
> > and m files. Fig1 has a Text Box and fig2 has a
Button. What
> > I would like to do is control something in fig1 by
doing
> > something in fig2 e.g. change the text in TextBox of
fig1 by
> > pressing the button in fig2. Can someone please tell
me how
> > I should go by it? I start in fig2 and then
> >
> > h = fig1;
> >
> > I see fig1 but I cannot control anything beyond
that... Any
> > suggestions?
> Hi Amol,
>
> You have to define the uicontrol handle as global so that
> you can control it from outside the file.
>
> for example:
> Fig1 m-file:
> function EditBox_CreateFcn(hObject, eventdata, handles)
> global hObject_EditBox
> hObject_EditBox = hObject;
>
> function EditBox_Callback(hObject, eventdata, handles)
> global EditBox
>
> Fig2 m-file:
> function Button_Callback(hObject, eventdata, handles)
> global hObject_EditBox
> set(hObject_EditBox, 'String', 'SUCCESS');
>
> i hope this will work, if not plz let me know.
>
> Ashwini
>

No need for globals here
You can get the other gui windows handles structure (if you
use guide and guidata..):
the_other_window_handles = guidata(otherfigure_handle);
To find the other figures handle assign Tag to your GUI
figures and:
findobj('Tag','Myfirstguifig_orwhatever_the_tag_is')

If you don't use guide and code the whole gui, you can
still manually save a struct of the handles to the userdata
of the figure. Or use guidata here too.

Or you can just add the Tag to your single button or edit
boxand find that with findobj.

Subject: Talking to multiple GUI

From: Amol

Date: 8 Feb, 2008 21:47:02

Message: 4 of 9

It works! Thanks. Althought we don't need the global
variable in EditBox_Callback. It works without it too.


"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in
message <fognl6$nc1$1@fred.mathworks.com>...
> "Amol " <amol.borkar@gmail.com> wrote in message
> <foglbe$o9o$1@fred.mathworks.com>...
> > I am having a lot of trouble in trying to talk to multiple
> > GUIs. I have created fig1 and fig2 which have seperate fig
> > and m files. Fig1 has a Text Box and fig2 has a Button. What
> > I would like to do is control something in fig1 by doing
> > something in fig2 e.g. change the text in TextBox of fig1 by
> > pressing the button in fig2. Can someone please tell me how
> > I should go by it? I start in fig2 and then
> >
> > h = fig1;
> >
> > I see fig1 but I cannot control anything beyond that... Any
> > suggestions?
> Hi Amol,
>
> You have to define the uicontrol handle as global so that
> you can control it from outside the file.
>
> for example:
> Fig1 m-file:
> function EditBox_CreateFcn(hObject, eventdata, handles)
> global hObject_EditBox
> hObject_EditBox = hObject;
>
> function EditBox_Callback(hObject, eventdata, handles)
> global EditBox
>
> Fig2 m-file:
> function Button_Callback(hObject, eventdata, handles)
> global hObject_EditBox
> set(hObject_EditBox, 'String', 'SUCCESS');
>
> i hope this will work, if not plz let me know.
>
> Ashwini
>

Subject: Talking to multiple GUI

From: Vihang Patil

Date: 9 Feb, 2008 06:43:02

Message: 5 of 9

"Amol " <amol.borkar@gmail.com> wrote in message
<foglbe$o9o$1@fred.mathworks.com>...
> I am having a lot of trouble in trying to talk to multiple
> GUIs. I have created fig1 and fig2 which have seperate fig
> and m files. Fig1 has a Text Box and fig2 has a Button. What
> I would like to do is control something in fig1 by doing
> something in fig2 e.g. change the text in TextBox of fig1 by
> pressing the button in fig2. Can someone please tell me how
> I should go by it? I start in fig2 and then
>
> h = fig1;
>
> I see fig1 but I cannot control anything beyond that... Any
> suggestions?


Another way of doing it.
doc setappdata
doc getappdata

I think there is a small example on the FEX too on this issue.
HTH
Vihang

Subject: Talking to multiple GUI

From: Loren Shure

Date: 11 Feb, 2008 20:50:37

Message: 6 of 9

In article <foglbe$o9o$1@fred.mathworks.com>, amol.borkar@gmail.com
says...
> I am having a lot of trouble in trying to talk to multiple
> GUIs. I have created fig1 and fig2 which have seperate fig
> and m files. Fig1 has a Text Box and fig2 has a Button. What
> I would like to do is control something in fig1 by doing
> something in fig2 e.g. change the text in TextBox of fig1 by
> pressing the button in fig2. Can someone please tell me how
> I should go by it? I start in fig2 and then
>
> h = fig1;
>
> I see fig1 but I cannot control anything beyond that... Any
> suggestions?
>

Nested functions can help you. See examples on matlabcentral or my
blog.

--
Loren
http://blogs.mathworks.com/loren/

Subject: Talking to multiple GUI

From: Amol

Date: 13 Apr, 2008 14:18:02

Message: 7 of 9

I got most of the multiple gui control working using the
global variables approach. Now I am trying to build a VCR
type application where I have the plot in one figure and the
VCR controls in the other figure. I am able to control
everything except display things on the plot since there
doesnt seem to be a call back where I can set a global
variable. Any suggestions?

Subject: Talking to multiple GUI

From: Ashwini Deshpande

Date: 14 Apr, 2008 07:05:04

Message: 8 of 9

"Amol " <amol.borkar@gmail.com> wrote in message
<ftt4mp$3p$1@fred.mathworks.com>...
> I got most of the multiple gui control working using the
> global variables approach. Now I am trying to build a VCR
> type application where I have the plot in one figure and the
> VCR controls in the other figure. I am able to control
> everything except display things on the plot since there
> doesnt seem to be a call back where I can set a global
> variable. Any suggestions?

Hai,

To define axes handles as global variable u need not have
callback function for it. Try out the following:
define the Tag of axes object as 'plot', and make its handle
as global parameter in opening function of the GUI

m_file 1:
function GUI_openingFcn(hObject, eventdata, handles)
global h_plot
h_plot = handles.plot;

m_file 2:
function myfile
global h_plot
axes(h_plot);
t = 0:0.01:1;
x = sin(2*pi*3*t);
plot(t,x) %%% now this signal will be plotted in the axes of
1st GUI.

Subject: Talking to multiple GUI

From: Amol

Date: 15 Apr, 2008 01:34:02

Message: 9 of 9

Works well. Thanks allot Ashwini! For some reason when I had
tried this same approach before, it failed. But it works
now. Thanks again for all the feedback from everyone.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us