How can I get the value from an inner callback function to an outer function?

Hi
I have a problem using GUI programming and callback functions in Matlab I have made a slider which is connected to an edit box, so that you can enter a value in the edit box and it will automatically change the value of the slider. It also does the reverse. I then want a push button which closes the figure window and returns the value of the slider, which I call ‘value’ in my function. But I can’t seem to figure out how to get the ‘value’ from the inner callback function to the outer function ‘slider’, so I can use it in my main script.
Can you tell me how to do? My function looks like this:
function value = slider
f = figure('Visible','off','Color','white','Position',...
[360, 500, 300, 300]);
minval = 0;
maxval = 100;
value = 0;
slhan = uicontrol('Style','slider','Position',[80,170,100,50],...
'Min',minval,'Max',maxval,'Callback',@callbackfn);
hmintext = uicontrol('Style','text','BackgroundColor','white',...
'Position',[40,175,30,30],'String',num2str(minval));
hmaxtext = uicontrol('Style','text','BackgroundColor','white',...
'Position',[190,175,30,30],'String',num2str(maxval));
hsttext = uicontrol('Style','text','BackgroundColor','white',...
'Position',[120,100,40,40],'Visible','off');
huitext = uicontrol('Style','edit','Position',[230, 182.5, 30, 30],...
'String',num2str(value),'Callback',@callbackfn);
hbutton = uicontrol('Style','pushbutton','String','Continue',...
'Position',[100, 50, 100, 50],'Callback',@callbackfn);
set(f,'Name','Slider')
movegui(f,'center')
set(f,'Visible','on');
function value = callbackfn(source,eventdata)
if source == slhan
value = get(slhan,'Value');
set(hsttext,'Visible','on','String',num2str(value))
set(huitext,'Visible','on','String',num2str(value))
elseif source == huitext
value = str2num(get(huitext,'String'));
if value > maxval
value = maxval;
elseif value < minval
value = minval;
elseif value >= minval && value <= maxval
end
set(slhan,'Value',value)
set(hsttext,'Visible','on','String',num2str(value))
set(huitext,'Visible','on','String',num2str(value))
else
value = get(slhan,'Value');
close(f)
end
end
end

Risposte (1)

assigin('caller','a',20)
creates a parameter a in the caller function and assigns 20 to it.
assigin('base','a',20)
does the same to the base workspace of MATLAB.

7 Commenti

in your application though it's opposite so you should use evalin().
Call the evalin function in the outer function and inquire the value of a specific parameter that already exists in the caller function (inner)
a_outer = evalin('caller',a_inner)
I tried using the evalin function
I put this in my outer function just above the inner function: outer = evalin('caller','value');
and Matlab returned this: ??? Error using ==> evalin Undefined function or variable 'value'.
Error in ==> slider at 34 outer = evalin('caller','value');
The variable 'value' should exist in the callback function but it seems like it doesn't. How do I fix this?
I'm sorry maybe I misunderstood your definition of outer and inner functions... there is a function (funcA) that calls another function (funcB). Function A is called the "caller" and B "is called".
If you want to bring valriable from A to B you need to use evalin('caller',...) in B.
If you want to bring variable from B to A you only need to set an output for function B and then the output of B will be available in A after B is run completely.
Here's an example of this:
function funcA
x=5;
a=2;
y = funcB(x);
w = y+z;
disp(w)
function y = funcB(x)
a = evalin('caller','a');
m = a+x;
y=2*m;
assignin('caller','z',m);
If I try to use the evalin function with any of the variables it returns the value when the figure window with the slider box opens up, but when I move the slider it gives me an error message saying that the variable does not exist.
I could make your example function work except for the assignin part. It displayed the correct value. But when I run my own function by setting a variable for the callback function the value displayed is always 0. And it doesn’t display the value after I close the figure but at the moment the figure window opens
function value2 = slider
...
value2 = callbackfn(huitext,[]);
disp(value2)
function value = callbackfn(source,eventdata)
value = get(slhan,'Value'); % when I click the pushbutton to close the window
It’s like the variables defined in the slider function disappears when it calls the callback function
whenever MATLAB is running any line of any code in any function, the variables that are accessible are all for that function. Meaning that the workspace variables are for that function.
As soon as MATLAB starts running the callback of slidebar, it changes the workspace. The main function (caller) calls the callback function to run and the workspace of the caller function is no longer accessible. During the time that the callback function is running, if you want to have access to any of the variables you need to use evalin('caller',...)
Okay, I see. However I still can't manage to make it work The assignin function should assign a value from the callback function to a variable in the the main function.
In the Command Window I can see that the value of the assigned variable changes when I use the slidebar.
But whenever I call the slider function from my main script I still get the value 0.
And the evalin function always tell me that the variable from the second input argument in evalin('caller',variable) doesn't exist even though it should be defined.
read the example I wrote above where I used assignin and evalin very carefully. You are probably making a mistake.. for example one typical mistake using these functions is that is your variable name is var1, you need to use assignin and evalin using 'var1' and not var1. In other words you need to use the char type of your double type variable.

Accedi per commentare.

Categorie

Scopri di più su Graphics Objects in Centro assistenza e File Exchange

Richiesto:

il 10 Apr 2013

Community Treasure Hunt

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

Start Hunting!

Translated by