From: Rony on
Hello all,

I'm making my first steps with developing a GUI for an application that I'm working on. I got stuck at the stage when I want to transmit some data or variables from one callback function to another.

I'll explain what I mean:
I've created a button named 'Load Image' that opens a dialog box which allows the user to select an image file, and then I read this image to a variable named 'img', using the 'imread' function.

At the next step I want to do some processing and operations on that image that was loaded, and I want this processing to begin when the user clicks on another button - let's call it 'Run'.

The problem is that the callback function of the 'Run' button "doesn't know" about the variable 'img' that was created at the 'Load Image' callback function, and so I cannot perform any actions on the image when I'm no within the function that's loaded that image.

I've tried defining the 'img' variable as a global variable, but this didn't help...

My question is, what is the correct syntax for gaining access to variables that were created inside another callback function?

Thanks a lot anyone who can help :)
From: Walter Roberson on
Rony wrote:

> My question is, what is the correct syntax for gaining access to
> variables that were created inside another callback function?

The only syntax-related method for this is to use nested functions

img = [];

function button1_cb(src, event)
...
img = imread(...)
end

function button2_cb(src, event)
... imhist(img); %e.g.
...
end

end



There are several other methods that do not use nested functions, such as
using guidata() .
From: someone on
"Rony " <rony.byalsky(a)gmail.com> wrote in message <huj9fe$i9$1(a)fred.mathworks.com>...
> Hello all,
>
> I'm making my first steps with developing a GUI for an application that I'm working on. I got stuck at the stage when I want to transmit some data or variables from one callback function to another.
>
> I'll explain what I mean:
> I've created a button named 'Load Image' that opens a dialog box which allows the user to select an image file, and then I read this image to a variable named 'img', using the 'imread' function.
>
> At the next step I want to do some processing and operations on that image that was loaded, and I want this processing to begin when the user clicks on another button - let's call it 'Run'.
>
> The problem is that the callback function of the 'Run' button "doesn't know" about the variable 'img' that was created at the 'Load Image' callback function, and so I cannot perform any actions on the image when I'm no within the function that's loaded that image.
>
> I've tried defining the 'img' variable as a global variable, but this didn't help...
>
> My question is, what is the correct syntax for gaining access to variables that were created inside another callback function?
>
> Thanks a lot anyone who can help :)

% Did you define img as global in BOTH callback functions (Run & Load Image)?

% Alternatively you could use the guidata structure (this is the prefered method).

doc guidata

From: Rony on
Thanks a lot to the both of you. Unfortunately I don't know much about nested functions or guidata, so I guess I'll have to do some reading.. :)
 | 
Pages: 1
Prev: File Conversion
Next: y-axes scalling