MATLAB Answers


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

Asked by Michael
on 10 Apr 2013
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],...
hmintext = uicontrol('Style','text','BackgroundColor','white',...
hmaxtext = uicontrol('Style','text','BackgroundColor','white',...
hsttext = uicontrol('Style','text','BackgroundColor','white',...
huitext = uicontrol('Style','edit','Position',[230, 182.5, 30, 30],...
hbutton = uicontrol('Style','pushbutton','String','Continue',...
'Position',[100, 50, 100, 50],'Callback',@callbackfn);
function value = callbackfn(source,eventdata)
if source == slhan
value = get(slhan,'Value');
elseif source == huitext
value = str2num(get(huitext,'String'));
if value > maxval
value = maxval;
elseif value < minval
value = minval;
elseif value >= minval && value <= maxval
value = get(slhan,'Value');


Sign in to comment.

1 Answer

Answer by Babak
on 10 Apr 2013

creates a parameter a in the caller function and assigns 20 to it.
does the same to the base workspace of MATLAB.


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.

Sign in to comment.