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
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!