Changing Value with Slider in App Designer

69 visualizaciones (últimos 30 días)
Erkin Karatas
Erkin Karatas el 9 de Dic. de 2019
Comentada: Rena Berman el 2 de Feb. de 2024
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

Respuesta aceptada

Jakob B. Nielsen
Jakob B. Nielsen el 9 de Dic. de 2019
Editada: Jakob B. Nielsen el 9 de Dic. de 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

Más respuestas (2)

Inso
Inso el 9 de Dic. de 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 comentarios
Erkin Karatas
Erkin Karatas el 9 de Dic. de 2019
Stil, no change :(
Inso
Inso el 9 de Dic. de 2019
Editada: Inso el 9 de Dic. de 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.

Iniciar sesión para comentar.


Jakob B. Nielsen
Jakob B. Nielsen el 9 de Dic. de 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 comentario
Erkin Karatas
Erkin Karatas el 9 de Dic. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Develop uifigure-Based Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by