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:
how to have a callback function under GUIDE be called, without having

Subject: how to have a callback function under GUIDE be called, without having

From: Nasser M. Abbasi

Date: 23 Dec, 2010 15:32:14

Message: 1 of 4


Under guide, I create a GUI, say t.m, and GUIDE creates many functions,
among them one called

% --- Executes just before t is made visible.
function t_OpeningFcn(hObject, eventdata, handles, varargin)


Now, after returning from the above function, the GUI becomes visible,
and I can click on a button, and then the callback associated with
that button will be called.

All well and good.

I have a reason to want another callback, say foo(), be called,
right after the GUI comes up, But without having an object
on the GUI associated with it to click on, for the callback foo()
to be called.

I want, somehow, for this callback to be called as if I did hit
on an button on the GUI, before anything else happens. This callback
should have the same call parameters as all the other callbacks.
(.i.e. hObject, eventdata, handles).

Is there a way to program this in?

Matlab 2010a, on windows.

thanks
--Nasser

Subject: how to have a callback function under GUIDE be called, without having

From: Sean de

Date: 23 Dec, 2010 16:19:05

Message: 2 of 4

"Nasser M. Abbasi" <nma@12000.org> wrote in message <ievq20$d31$1@speranza.aioe.org>...
>
> Under guide, I create a GUI, say t.m, and GUIDE creates many functions,
> among them one called
>
> % --- Executes just before t is made visible.
> function t_OpeningFcn(hObject, eventdata, handles, varargin)
>
>
> Now, after returning from the above function, the GUI becomes visible,
> and I can click on a button, and then the callback associated with
> that button will be called.
>
> All well and good.
>
> I have a reason to want another callback, say foo(), be called,
> right after the GUI comes up, But without having an object
> on the GUI associated with it to click on, for the callback foo()
> to be called.
>
> I want, somehow, for this callback to be called as if I did hit
> on an button on the GUI, before anything else happens. This callback
> should have the same call parameters as all the other callbacks.
> (.i.e. hObject, eventdata, handles).
>
> Is there a way to program this in?
>
> Matlab 2010a, on windows.
>
> thanks
> --Nasser
>
>

That's what the t_OutputFcn, also generated automatically by GUIDE, does!

Subject: how to have a callback function under GUIDE be called, without having

From: Nasser M. Abbasi

Date: 23 Dec, 2010 23:50:02

Message: 3 of 4

On Dec 23, 8:19 am, "Sean de "
<sean.dewol...@nospamplease.umit.maine.edu> wrote:


> That's what the t_OutputFcn, also generated automatically by GUIDE, does!

Thanks Sean.

But the above seems to be half the story. But it is very good start
for me, as at least now I have the callback I wanted.

Reading the documentation, it says:

"Output Function

The output function returns, to the command line, outputs that are
generated during its execution. It is executed when the opening
function returns control and before control returns to the command
line."

So, I can use OutputFcn() for what I want, to add some logic there.

But notice what it says, that this same function is also called again,
at least this is how I read it, i.e. it is also called when the GUI is
closing down. ie. when I click to close the GUI, i.e. click on the 'X'
on the corner of the GUI to close the figure window.

Hence I need to know, when I am inside OutputFcn(), if this is the
first call (i.e. the one after OpeningFnc(), or if it being called
because the GUI is closing. Because I wanted to do something
different depending on which case it is.

I'll try to see if there is a context I can use to tell me which of
the 2 cases it is.

The documentation above could have been written more clearly. It
should read something like

"It is executed when the opening function returns control <and also>
before control returns to the command line."

To make it more clear.

Any way, I am just learning the use of this function, as I have not
used it before.

thanks for your help.

--Nasser

Subject: how to have a callback function under GUIDE be called, without

From: Nasser M. Abbasi

Date: 24 Dec, 2010 03:37:21

Message: 4 of 4

On 12/23/2010 3:50 PM, Nasser M. Abbasi wrote:
>

>
> I'll try to see if there is a context I can use to tell me which of
> the 2 cases it is.

Ok, problem solved.

I was running a loop inside OutputFcn(), a state machine loop, and
doing data=guidata(hObject) in each step.

The problem is when the the GUI is closing, hObject was no longer
valid.

So I simply needed to check if the handle is still valid or not,
to terminate the loop.

Now it is working well.

issue closed.

--Nasser

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