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

조회 수: 3 (최근 30일)
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

답변 (1개)

Babak
Babak 2013년 4월 10일
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
Michael
Michael 2013년 4월 14일
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.
Babak
Babak 2013년 4월 15일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by