passing values between functions in GUI

8 Ansichten (letzte 30 Tage)
edward
edward am 11 Aug. 2011
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

Akzeptierte Antwort

Image Analyst
Image Analyst am 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 Kommentare
edward
edward am 16 Aug. 2011
Paulo,
I can't express enough my heartfelt appreciation for your efforts...I have spent the past 5 days reading documentation on matlab and reviewing, re-reviewing your code...I feel more clear about the issue and certainly much more educated about the requirements. I've copied your code and have edited until I am cross-eyed and have gotten closer...there is like 4 different ways to proceed and it seems I am continuing to confound them...there is so much foundational stuff I get but don't get...it has been a trip to have your input to have pushed me along so at least i can somewhat understand the issue...I am continuing to try different approaches until I am completely worn out and then will likely bug you for some additional help...I get close but still no cigar...it seems my problem is trying to take your code (programmatic) and merge it with the GUI /GUIDE environment and I run into problems...just wanted to take a break from pounding on this to express my appreciation and to ask if I can request some more help in the future?
Paulo Silva
Paulo Silva am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Paulo Silva
Paulo Silva am 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

Kategorien

Mehr zu App Building 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!

Translated by