MATLAB: Changing Value with Slider in App Designer

appdesignerchange value with sliderguislider

1

Best Answer

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