How to Set The Max Value of GUI slider to a Calculated Value

1 次查看(过去 30 天)
How do I set the Max value of a slider to a calculated value using the GUI builder in Matlab.
I am trying to get the slider to equal Df which is the max distance travelled.
classdef Major1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Button matlab.ui.control.Button
Slider matlab.ui.control.Slider
SliderLabel matlab.ui.control.Label
AngleofTrigectoryEditField matlab.ui.control.NumericEditField
AngleofTrigectoryEditFieldLabel matlab.ui.control.Label
IntialVelocityEditField matlab.ui.control.NumericEditField
IntialVelocityEditFieldLabel matlab.ui.control.Label
HeightEditField matlab.ui.control.NumericEditField
HeightEditFieldLabel matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function Calc(app, event)
H=app.HeightEditField.Value;
U=app.IntialVelocityEditField.Value;
theta=app.AngleofTrigectoryEditField.Value;
Uy=U*sind(theta)
Tf=(-Uy-sqrt(Uy^2-4*(1/2)*-9.81*H))/(-9.81)
t = linspace(Tf,0,50);
y=(1/2)*-9.81*t.^2+Uy*t+H
x=(U*cosd(theta))*t
plot (app.UIAxes,x,y)
end
% Value changing function: Slider
function SliderValueChanging(app, event)
D = event.Value;
H=app.HeightEditField.Value;
U=app.IntialVelocityEditField.Value;
theta=app.AngleofTrigectoryEditField.Value;
Uy=sind(theta)*U;
Tf=(-Uy-sqrt(Uy^2-4*(1/2)*-9.81*H))/(-9.81);
Df=(U*cosd(theta))*Tf;
Ts=D/(U*cosd(theta))
Hy=(1/2)*-9.81*Ts.^2+Uy*Ts+H;
plot(app.UIAxes,D,Hy,'*','MarkerEdgeColor',"r",'MarkerSize',12)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 868 734];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.ColorOrder = [0 0.447058823529412 0.741176470588235];
app.UIAxes.LineStyleOrder = {'--'};
app.UIAxes.NextPlot = 'add';
app.UIAxes.Position = [105 122 640 355];
% Create HeightEditFieldLabel
app.HeightEditFieldLabel = uilabel(app.UIFigure);
app.HeightEditFieldLabel.HorizontalAlignment = 'right';
app.HeightEditFieldLabel.Position = [150 698 40 22];
app.HeightEditFieldLabel.Text = 'Height';
% Create HeightEditField
app.HeightEditField = uieditfield(app.UIFigure, 'numeric');
app.HeightEditField.Position = [205 698 100 22];
% Create IntialVelocityEditFieldLabel
app.IntialVelocityEditFieldLabel = uilabel(app.UIFigure);
app.IntialVelocityEditFieldLabel.HorizontalAlignment = 'right';
app.IntialVelocityEditFieldLabel.Position = [115 662 75 22];
app.IntialVelocityEditFieldLabel.Text = 'Intial Velocity';
% Create IntialVelocityEditField
app.IntialVelocityEditField = uieditfield(app.UIFigure, 'numeric');
app.IntialVelocityEditField.Position = [205 662 100 22];
% Create AngleofTrigectoryEditFieldLabel
app.AngleofTrigectoryEditFieldLabel = uilabel(app.UIFigure);
app.AngleofTrigectoryEditFieldLabel.HorizontalAlignment = 'right';
app.AngleofTrigectoryEditFieldLabel.Position = [85 624 105 22];
app.AngleofTrigectoryEditFieldLabel.Text = 'Angle of Trigectory';
% Create AngleofTrigectoryEditField
app.AngleofTrigectoryEditField = uieditfield(app.UIFigure, 'numeric');
app.AngleofTrigectoryEditField.Position = [205 624 100 22];
% Create SliderLabel
app.SliderLabel = uilabel(app.UIFigure);
app.SliderLabel.HorizontalAlignment = 'right';
app.SliderLabel.Position = [85 101 36 22];
app.SliderLabel.Text = 'Slider';
% Create Slider
app.Slider = uislider(app.UIFigure);
app.Slider.ValueChangingFcn = createCallbackFcn(app, @SliderValueChanging, true);
app.Slider.Position = [142 110 603 3];
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @Calc, true);
app.Button.Position = [205 582 100 23];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Major1
% Create UIFigure and 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

回答(1 个)

Antoni Garcia-Herreros
Try adding this line of code when you are defining the Slider
app.Slider.Limits=[0 Df];

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by