Clear Filters
Clear Filters

Changing Value with Slider in App Designer

44 views (last 30 days)
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

Accepted Answer

Jakob B. Nielsen
Jakob B. Nielsen on 9 Dec 2019
Edited: Jakob B. Nielsen on 9 Dec 2019
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

More Answers (2)

Inso
Inso on 9 Dec 2019
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 Comments
Inso
Inso on 9 Dec 2019
Edited: Inso on 9 Dec 2019
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.

Sign in to comment.


Jakob B. Nielsen
Jakob B. Nielsen on 9 Dec 2019
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
  1 Comment
Erkin Karatas
Erkin Karatas on 9 Dec 2019
Unfortunately, I still get the same plot, I am posting the whole code, so that you can see if anything is wrong
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;
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(app, event)
global n
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
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, @GenerateButtonPushed, true);
app.RPMSlider.Position = [104 232 160 3];
app.RPMSlider.Value = 140;
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

Sign in to comment.

Categories

Find more on Develop uifigure-Based Apps 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!