Changing Value with Slider in App Designer
39 次查看(过去 30 天)
显示 更早的评论
function GenerateButtonPushed(app, event)
global a
global V_f
global L
global S
global n
global D
a = 10;
V_f = 15;
L = 50;
S =120;
n = 140;
D=50;
x = 1:50;
y = 1:50;
V_w = (V_f * pi * D * n)/(2*L*S);
for i = 1:length(y)
hor(i) = V_w * sqrt(2*y(i)/a);
i = i+1;
end
plot(y,hor, 'r')
hold on
for i=1:100;
k = y - i;
plot(k, hor, 'r')
hold on
m = -x - i;
plot(m, hor, 'b')
hold on
i= i +1;
end
end
Hi all, I have implemented this code into app designer, and I want to change the 'n' value with slider.
function RPMSliderValueChanging(app, event)
changingValue = event;
n = changingValue;
end
I have created a callback for adjusting the n value. I want to change the n value and then plot the updated figure. However, the plot does not change when I change the value with slider. Can someone please help me. Thanks
采纳的回答
Jakob B. Nielsen
2019-12-9
编辑:Jakob B. Nielsen
2019-12-9
Ah, I misread your first post. But your second post clarifies everything. You have two problems:
On your line 103, your callback function to the slider value change is named GenerateButtonPushed. But in line 60, you call it as RPMSliderValueChanging - and this function does not exist, so you will not get any new n values read.
In addition, in your actual GenerateButtonPushed function, line 24, you set n=140 every time you push the generate button, so even if the slider read a new n, when you push the button you set n=140 again. Define n outside of your function - for example, on line 105, where you define all the GUI options is a good place to "initialize values".
This also means my first suggestion can be deleted - I have done this, and introduced the two other things, it seems to work for me although your axes are a little restrictive :)
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
GenerateButton matlab.ui.control.Button
RPMSliderLabel matlab.ui.control.Label
RPMSlider matlab.ui.control.Slider
end
methods (Access = private)
% Callback function: GenerateButton, RPMSlider
function GenerateButtonPushed(app, event)
global a
global V_f
global L
global S
global n
global D
a = 10;
V_f = 15;
L = 50;
S =120;
%n =140; If you have the n here, it is hardset to 140 every time you push the button
D=50;
x = 1:50;
y = 1:50;
V_w = (V_f * pi * D * n)/(2*L*S);
for i = 1:length(y)
hor(i) = V_w * sqrt(2*y(i)/a);
i = i+1;
end
plot(y,hor, 'r')
hold on
for i=1:100
k = y - i;
plot(k, hor, 'r')
hold on
m = -x - i;
plot(m, hor, 'b')
hold on
i= i +1;
end
hold off
if L<75
axis([-35 -10 20 34])
elseif L<200
axis([-35 -10 8 20])
elseif L<350
axis([-35 -10 3.5 6])
elseif L>800
axis([-35 -15 1.5 2.5])
else
axis([-35 -10 2 4])
end
set(gca,'xtick',[])
set(gca,'ytick',[])
end
% Callback function
function RPMSliderValueChanging(~, event)
global n
changingValue = event ;
n = changingValue.Value ;
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 368 359];
app.UIFigure.Name = 'UI Figure';
% Create GenerateButton
app.GenerateButton = uibutton(app.UIFigure, 'push');
app.GenerateButton.ButtonPushedFcn = createCallbackFcn(app, @GenerateButtonPushed, true);
app.GenerateButton.Position = [119 89 100 22];
app.GenerateButton.Text = 'Generate';
% Create RPMSliderLabel
app.RPMSliderLabel = uilabel(app.UIFigure);
app.RPMSliderLabel.HorizontalAlignment = 'right';
app.RPMSliderLabel.Position = [61 223 32 22];
app.RPMSliderLabel.Text = 'RPM';
% Create RPMSlider
app.RPMSlider = uislider(app.UIFigure);
app.RPMSlider.Limits = [0 250];
app.RPMSlider.ValueChangingFcn = createCallbackFcn(app, @RPMSliderValueChanging, true); %The function must be named rightly :)
app.RPMSlider.Position = [104 232 160 3];
app.RPMSlider.Value = 140;
global n
n=140; %Define your n outside of the button push function
end
end
methods (Access = public)
% Construct app
function app = app1
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
更多回答(2 个)
Inso
2019-12-9
This is because 'n' is not a global variable in RPMSliderValueChanging function.
You can either claim n to be global in the callback function or create n as a property of you app to pass the data.
4 个评论
Inso
2019-12-9
编辑:Inso
2019-12-9
Sorry I mistype something. It should be:
function RPMSliderValueChanging(app, event)
global n
changingValue = event;
n = changingValue;
cla(gca)
GenerateButtonPushed(app, event)
end
Also assign all the global variables in the Startup function instead of in GenerateButtonPushed.
Jakob B. Nielsen
2019-12-9
Currently, all your slider event function does is read a new value of n. The figure will only change if you specifcy that it must, so include that in your slider value change event, and see if that works.
function RPMSliderValueChanging(app, event)
changingValue = event;
n = changingValue;
V_w = (V_f * pi * D * n)/(2*L*S);
for i = 1:length(y)
hor(i) = V_w * sqrt(2*y(i)/a);
i = i+1;
end
plot(y,hor, 'r')
hold on
for i=1:100;
k = y - i;
plot(k, hor, 'r')
hold on
m = -x - i;
plot(m, hor, 'b')
hold on
i= i +1;
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!