How can I get the value from an inner callback function to an outer function?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Babak
am 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 Kommentare
Babak
am 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.
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!