How to compute given values in the edit text box with a formula just by using a button?
3 views (last 30 days)
Show older comments
This is the case statement of the program I would like to accomplish.
- Ask for the value of different variables such as (Initial Velocity, Initial Height, Angle)
- Choose what type of unit using the drop down menu (MKS or FPS)
- Push a button to provide values for (Maximum Height, Distance, Time of Flight, Gravity and to Plot)
My code is here attached. classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
InitialVelocitymsEditFieldLabel matlab.ui.control.Label
InitialVelocitymsEditField matlab.ui.control.NumericEditField
InitialHeightmEditFieldLabel matlab.ui.control.Label
InitialHeightmEditField matlab.ui.control.NumericEditField
AngledegreesEditFieldLabel matlab.ui.control.Label
AngledegreesEditField matlab.ui.control.NumericEditField
UnitDropDownLabel matlab.ui.control.Label
UnitDropDown matlab.ui.control.DropDown
MaximumHeightmEditFieldLabel matlab.ui.control.Label
MaximumHeightmEditField matlab.ui.control.NumericEditField
DistancemEditFieldLabel matlab.ui.control.Label
DistancemEditField matlab.ui.control.NumericEditField
TimeofFlightsEditFieldLabel matlab.ui.control.Label
TimeofFlightsEditField matlab.ui.control.NumericEditField
GravitymsEditFieldLabel matlab.ui.control.Label
GravitymsEditField matlab.ui.control.NumericEditField
PlotButton matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: PlotButton
function PlotButtonPushed(app, event)
v = str2char(input(app.InitialVelocitymsEditField,'string'));
h = str2char(input(app.InitialHeightmEditField,'string'));
a = str2char(input(app.AngledegreesEditField,'string'));
d = str2char(input(app.DistancemEditField,'string'));
if ~isempty(v) && ~isempty(h) && ~isempty(a)
d = v.^(2).*(sin(a))/app.GravitymsEditField;
end
value = app.UnitDropDown.Value;
if strcmpi(value,'FPS')
app.GravitymsEditField.Value =-9.81;
else if strcmpi(value,'MKS')
app.GravitymsEditField.Value =-32.174;
end
end
end
% Value changed function: MaximumHeightmEditField
function MaximumHeightmEditFieldValueChanged(app, event)
value = app.MaximumHeightmEditField.Value;
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 772 643];
app.UIFigure.Name = 'MATLAB App';
% Create InitialVelocitymsEditFieldLabel
app.InitialVelocitymsEditFieldLabel = uilabel(app.UIFigure);
app.InitialVelocitymsEditFieldLabel.Tag = 'v';
app.InitialVelocitymsEditFieldLabel.HorizontalAlignment = 'right';
app.InitialVelocitymsEditFieldLabel.Position = [8 521 109 22];
app.InitialVelocitymsEditFieldLabel.Text = 'Initial Velocity (m/s)';
% Create InitialVelocitymsEditField
app.InitialVelocitymsEditField = uieditfield(app.UIFigure, 'numeric');
app.InitialVelocitymsEditField.Tag = 'v0';
app.InitialVelocitymsEditField.Position = [132 521 100 22];
% Create InitialHeightmEditFieldLabel
app.InitialHeightmEditFieldLabel = uilabel(app.UIFigure);
app.InitialHeightmEditFieldLabel.Tag = 'h';
app.InitialHeightmEditFieldLabel.HorizontalAlignment = 'right';
app.InitialHeightmEditFieldLabel.Position = [24 491 93 22];
app.InitialHeightmEditFieldLabel.Text = 'Initial Height (m)';
% Create InitialHeightmEditField
app.InitialHeightmEditField = uieditfield(app.UIFigure, 'numeric');
app.InitialHeightmEditField.Tag = 'h';
app.InitialHeightmEditField.Position = [132 491 100 22];
% Create AngledegreesEditFieldLabel
app.AngledegreesEditFieldLabel = uilabel(app.UIFigure);
app.AngledegreesEditFieldLabel.HorizontalAlignment = 'right';
app.AngledegreesEditFieldLabel.Position = [26 460 91 22];
app.AngledegreesEditFieldLabel.Text = 'Angle (degrees)';
% Create AngledegreesEditField
app.AngledegreesEditField = uieditfield(app.UIFigure, 'numeric');
app.AngledegreesEditField.Tag = 'a';
app.AngledegreesEditField.Position = [132 460 100 22];
% Create UnitDropDownLabel
app.UnitDropDownLabel = uilabel(app.UIFigure);
app.UnitDropDownLabel.HorizontalAlignment = 'right';
app.UnitDropDownLabel.Position = [70 420 30 22];
app.UnitDropDownLabel.Text = 'Unit ';
% Create UnitDropDown
app.UnitDropDown = uidropdown(app.UIFigure);
app.UnitDropDown.Items = {'FPS', 'MKS'};
app.UnitDropDown.Position = [115 420 100 22];
app.UnitDropDown.Value = 'FPS';
% Create MaximumHeightmEditFieldLabel
app.MaximumHeightmEditFieldLabel = uilabel(app.UIFigure);
app.MaximumHeightmEditFieldLabel.Tag = 'mh';
app.MaximumHeightmEditFieldLabel.HorizontalAlignment = 'right';
app.MaximumHeightmEditFieldLabel.Position = [16 374 117 22];
app.MaximumHeightmEditFieldLabel.Text = 'Maximum Height (m)';
% Create MaximumHeightmEditField
app.MaximumHeightmEditField = uieditfield(app.UIFigure, 'numeric');
app.MaximumHeightmEditField.ValueChangedFcn = createCallbackFcn(app, @MaximumHeightmEditFieldValueChanged, true);
app.MaximumHeightmEditField.Tag = 'mh';
app.MaximumHeightmEditField.Position = [148 374 100 22];
% Create DistancemEditFieldLabel
app.DistancemEditFieldLabel = uilabel(app.UIFigure);
app.DistancemEditFieldLabel.Tag = 'd';
app.DistancemEditFieldLabel.HorizontalAlignment = 'right';
app.DistancemEditFieldLabel.Position = [59 341 74 22];
app.DistancemEditFieldLabel.Text = 'Distance (m)';
% Create DistancemEditField
app.DistancemEditField = uieditfield(app.UIFigure, 'numeric');
app.DistancemEditField.Tag = 'd';
app.DistancemEditField.Position = [148 341 100 22];
% Create TimeofFlightsEditFieldLabel
app.TimeofFlightsEditFieldLabel = uilabel(app.UIFigure);
app.TimeofFlightsEditFieldLabel.HorizontalAlignment = 'right';
app.TimeofFlightsEditFieldLabel.Position = [38 311 95 22];
app.TimeofFlightsEditFieldLabel.Text = 'Time of Flight (s)';
% Create TimeofFlightsEditField
app.TimeofFlightsEditField = uieditfield(app.UIFigure, 'numeric');
app.TimeofFlightsEditField.Tag = 't';
app.TimeofFlightsEditField.Position = [148 311 100 22];
% Create GravitymsEditFieldLabel
app.GravitymsEditFieldLabel = uilabel(app.UIFigure);
app.GravitymsEditFieldLabel.HorizontalAlignment = 'right';
app.GravitymsEditFieldLabel.Position = [54 280 80 22];
app.GravitymsEditFieldLabel.Text = 'Gravity (m/s^)';
% Create GravitymsEditField
app.GravitymsEditField = uieditfield(app.UIFigure, 'numeric');
app.GravitymsEditField.Tag = 'g';
app.GravitymsEditField.Position = [149 280 99 22];
% Create PlotButton
app.PlotButton = uibutton(app.UIFigure, 'push');
app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
app.PlotButton.Position = [93 240 100 22];
app.PlotButton.Text = 'Plot';
% 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.Position = [290 341 392 261];
% 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 = app1
% 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
0 Comments
Answers (1)
Rishabh Singh
on 29 Nov 2021
Hey Carlo,
According to me you wish to calculate few parameter based upon the input fields. I would suggest few minor corrections to your code for function "PlotButtonPushed". More information can be found on following documentation.
v = app.InitialVelocitymsEditField.Value;
h = app.InitialHeightmEditField.Value;
a = deg2rad(app.AngledegreesEditField.Value);
d = app.DistancemEditField.Value;
if(strcmpi(app.UnitDropDown.Value, 'FPS'))
app.GravitymsEditField.Value = -9.81;
elseif strcmpi(app.UnitDropDown.Value, 'MKS')
app.GravitymsEditField.Value =-32.174;
end
app.DistancemEditField.Value = v.^(2).*(sin(a))/app.GravitymsEditField.Value;
You could add your further calculations for "Maximum Height", "Time of Flight" in a similar fashion.
Currently I am not sure of what exactly you want to plot, if you could provide more information then I will be able to help you further.
Hope this helps.
0 Comments
See Also
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!