passing values between functions in GUI
8 views (last 30 days)
Show older comments
have an animation plot in a GUI that is excuted by pressing a button. It works just fine. I want to be able to control the speed of the plot refresh so I put a 'pause' function inside the loop for the plot...see snippet next for k=3:length(Y) z=t(:,1:k) set(h,'Zdata',z) drawnow pause(2) this works also but I want to be able to control the speed from a slider on the GUI. How do I pass the value from the slider into the pause function? Do i have to call the slider function from within the parenthesis of the pause function...I am a novice to Matlab so will need patience. I can get the slider value to show in the command window but that is all?, help
0 Comments
Accepted Answer
Image Analyst
on 11 Aug 2011
Have you seen the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F? It lists several ways to share variables. Pick your favorite.
2 Comments
Paulo Silva
on 16 Aug 2011
I don't like global variables but you could use one of them like I did on this file http://www.mathworks.com/matlabcentral/fileexchange/29618-spspj , good luck
More Answers (1)
Paulo Silva
on 11 Aug 2011
One example, first one allows you to select the argument of the pause function via a slider, it's the time in seconds (values from 0 to 1)
function testGUI1
f=figure;
fp=get(f,'Position');
set(f,'Position',[fp(1)-200 fp(2)-100 fp(3)-300 fp(4)-300])
h = uicontrol('Style', 'slider',...
'Position', [20 10 100 70], 'Callback', @smv);
set(h,'tag','sliderGUI1')
function smv(obj,event)
hsld=findall(0,'tag','pauseGUI2');
if ~isempty(hsld)
t=num2str(get(h,'value'));
else
t='0';
end
set(hsld,'String',['pause(' t ')'])
end
end
Second GUI shows the argument of the delay function (the value of the slider), and a button allows a test to run, when pressed it counts from 1 to 10 using the pause argument selected on the slider.
function testGUI2
f=figure;
fp=get(f,'Position');
set(f,'Position',[fp(1)+100 fp(2)-100 fp(3)-200 fp(4)-200])
ht = uicontrol('Style', 'text','string','0',...
'Position', [20 150 100 70]);
hsld=findall(0,'tag','sliderGUI1');
if ~isempty(hsld)
t=num2str(get(hsld,'value'));
else
t='0';
end
ht1 = uicontrol('Style', 'text','string',['pause(' t ')'],...
'Position', [20 70 100 70]);
set(ht1,'tag','pauseGUI2')
hb = uicontrol('Style', 'pushbutton','string','Start',...
'Position', [140 150 100 70], 'Callback', @pbt);
function pbt(obj,event)
for n=1:10
if ~isempty(hsld)
set(ht,'string',n)
pause(get(hsld,'value'))
else
pause(0)
end
end
end
end
Save both GUIs and run them:
testGUI1;testGUI2
Edit: for some unknown reason I missed your question title, but the way I used is very similar to what you can do
Here's all just in one GUI (notice the use of subfunctions)
function testGUI3
f=figure;
fp=get(f,'Position');
set(f,'Position',[fp(1)+100 fp(2)-100 fp(3)-200 fp(4)-200])
ht = uicontrol('Style', 'text','string','0',...
'Position', [20 150 100 70]);
hs = uicontrol('Style', 'slider',...
'Position', [140 70 100 70], 'Callback', @smv);
ht1 = uicontrol('Style', 'text','string',['pause(' '0' ')'],...
'Position', [20 70 100 70]);
hb = uicontrol('Style', 'pushbutton','string','Start',...
'Position', [140 150 100 70], 'Callback', @pbt);
function smv(obj,event)
t=num2str(get(hs,'value'));
set(ht1,'String',['pause(' t ')'])
end
function pbt(obj,event)
%test code
for n=1:10
set(ht,'string',n)
pause(get(hs,'value'))
end
%end test code
end
end
If you are using GUIDE to make your GUI, instead of pause(get(hs,'value')) use pause(get(handles.slider,'value')) in any GUI function made by GUIDE (replace slider by the name of your slider if you changed the name or have more than one slider).
My example might be different from what you are doing so where you have
for k=3:length(Y)
z=t(:,1:k);
set(h,'Zdata',z);
drawnow %might not be required
pause(2)
end
use this instead
for k=3:length(Y)
z=t(:,1:k);
set(h,'Zdata',z);
drawnow %might not be required
pause(get(handles.slider,'value'))
end
0 Comments
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!