How can I get the value from an inner callback function to an outer function?
Mostra commenti meno recenti
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)
Babak
il 10 Apr 2013
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)
Michael
il 10 Apr 2013
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);
Babak
il 12 Apr 2013
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',...)
Michael
il 14 Apr 2013
Babak
il 15 Apr 2013
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.
Categorie
Scopri di più su Graphics Objects in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!