フィルターのクリア

How can I make it so that the value from my slider changes the rotation speed of the ferris wheel in my code?

3 ビュー (過去 30 日間)
Hi, I am trying to write code so that I can slide the slider and the value it corresponds to is the value p in my code which changes the rotation speed of the ferris wheel. I'm not sure where I went wrong in the code. Thanks!
slider = uicontrol('Style','slider','Min',0,'Max',100,'Value',0, 'SliderStep',[0.1 0.1], ...
'Units', 'normalized', 'Position', [0.1 0.01 0.8 0.03], Callback, {@sliderCallback});
p=sliderCallback(slider,'Value');
where p plays a part here in the movement of my ferris wheel: the smaller p is, the smaller amount of time each frame will pause for therefore it will seem like the baskets on my ferris wheel are rotating faster.
moving_basket2=translateShape(basket2,-10*cos(n+(pi/2)),-10*sin(n+(pi/2))-10); % Creates fourth basket
drawshape(moving_basket2,"m");
fill(moving_basket2(1,:),moving_basket2(2,:),"m")
axis([-18 28 -18 18]) % Sets axis by defining xmin, xmax, ymin, ymax
pause(p) % Animates the baskets
hold off % Ensures that only one frame of each basket is displayed at a time
end
my sliderCallback function is:
function a=sliderCallback(slider,event)
val=get(slider,'Value');
a=val
end

回答 (2 件)

VBBV
VBBV 2023 年 12 月 19 日
Delete the return value for sliderCallback function call as shown below and put the while and/or for loop code for the animation of baskets inside the sliderCallback function
s = uicontrol('Style','slider','Min',0,'Max',100,'Value',0, 'SliderStep',[0.1 0.1], ...
'Units', 'normalized', 'Position', [0.1 0.01 0.8 0.03]);
s.Callback = @sliderCallback % call the function for slider value
function sliderCallback(slider,event) % delete the return value for sliderCallback function
val=get(slider,'Value');
p=val
% use the value of p in the loop to animate the baskets
%while 1 % put the loop part in your code inside the slidercallback
%function
moving_basket2=translateShape(basket2,-10*cos(n+(pi/2)),-10*sin(n+(pi/2))-10); % Creates fourth basket
drawshape(moving_basket2,"m");
fill(moving_basket2(1,:),moving_basket2(2,:),"m")
axis([-18 28 -18 18]) % Sets axis by defining xmin, xmax, ymin, ymax
pause(p) % Animates the baskets
hold off % Ensures that only one frame of each basket is displayed at a time
%end
end
  2 件のコメント
Vicky
Vicky 2023 年 12 月 19 日
Do I need to actually use the function sliderCallback somewhere in my main script? If so, what actually is 'event'?
Or is line in the main script the only place sliderCallback needs to appear?
s.Callback = @sliderCallback % call the function for slider value
in the main script the only place sliderCallback needs to appear?
VBBV
VBBV 2023 年 12 月 20 日
If so, what actually is 'event'?
event detects the action done with App or GUI component by user.
Do I need to actually use the function sliderCallback somewhere in my main script?
Or is line in the main script the only place sliderCallback needs to appear?
sliderCallback function can be invoked from the mainscript file. You can either put the function in the main script file or outside the main script file. If you place function code outside the mainscript, then all the related functions need to be put in the same folder, otherwise you would get path errors.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2023 年 12 月 18 日
slider = uicontrol('Style','slider','Min',0,'Max',100,'Value',0, 'SliderStep',[0.1 0.1], ...
'Units', 'normalized', 'Position', [0.1 0.01 0.8 0.03], Callback, {@sliderCallback});
The initialization of slider sets sliderCallback as the callback function. When that function is invoked because of a callback, any value it returns will be ignored.
p=sliderCallback(slider,'Value');
That will call the function once and will probably return a value of 0 because that is what the properties are initialized to.
pause(p) % Animates the baskets
You need to have that p updated every iteration of the loop, so before that line you should add in
p = slider.Value;
  1 件のコメント
Vicky
Vicky 2023 年 12 月 19 日
Thanks! But it still doesn't work for some reason, I get an error in the line slider=uicontrol.....
Is there something wrong with the code of my sliderCallback function?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by