setting up graphic user interface

5 次查看(过去 30 天)
Jon
Jon 2023-6-7
回答: Divyanshu 2023-6-16
As i have never set up a GUI interface I am trying to find out how to do all the call backs correctly. I have included the program I wrote in script editor and the GUI code program and a screen shot of the design of the GUI. My question is can the radio buttons be used with the code I wrote in editor with all the different loops that I have and if so how do I do that code? If the radio buttons can not work that way is there a better choice?
%Program in editor
clear
clc
F=input('gasoline or Diesel: ','s');
C=input('number of engine cylinders ');
T=input('trailer being towed yes or no: ','s');
TW=input('trailer weight ');
M=input('trip length in miles ');
c=input('cost of fuel per gallon ');
switch F
case 'gasoline'
if C==4
bmpg=24;
switch T
case 'yes'
if TW<=2000
mpg=bmpg-(bmpg*0.2);
elseif TW>2000
fprintf('exceedes truck weight limit ');
end
case 'no'
mpg=24;
end
elseif C==6
bmpg=18;
switch T
case 'yes'
if TW<=2000
mpg=bmpg-(bmpg*0.1);
elseif TW>2000 && TW<=4000
mpg=bmpg-(bmpg*0.2);
elseif TW>4000
fprintf('exceedes truck weight limit ');
end
case 'no'
mpg=18;
end
elseif C==8
bmpg=12;
switch T
case'yes'
if TW<=2000
mpg=bmpg-(bmpg*0.05);
elseif TW>2000 && TW<=4000
mpg=bmpg-(bmpg*0.1);
elseif TW>4000 && TW<=6000
mpg=bmpg-(bmpg*0.2);
elseif TW>6000 && TW<=8000
mpg=bmpg-(bmpg*0.4);
elseif TW>8000
fprintf('exceedes truck weight limit ');
end
case'no'
mpg=12;
end
end
case 'diesel'
if C==4
bmpg=26;
switch T
case'yes'
if TW<=2000
mpg=bmpg-(bmpg*0.05);
elseif TW>2000 && TW<=4000
mpg=bmpg-(bmpg*0.1);
elseif TW>4000
fprintf('exceedes truck weight limit ');
end
case 'no'
mpg=26;
end
elseif C==6
bmpg=22;
switch T
case'yes'
if TW<=2000
mpg=bmpg-(bmpg*0.025);
elseif TW>2000 && TW<=4000
mpg=bmpg-(mbpg*0.5);
elseif TW>4000 && TW<=6000
mpg=bmpg-(bmpg*0.1);
elseif TW>6000 && TW<=8000
mpg=bmpg-(bmpg*0.2);
elseif TW>8000
fprintf('exceedes truck weight limit ');
end
case'no'
mpg=22;
end
elseif C==8
bmpg=18;
switch T
case'yes'
if TW<=2000
mpg=bmpg-(bmpg*0.0125);
elseif TW>2000 && TW<=4000
mpg=bmpg-(bmpg*0.05);
elseif TW>4000 && TW<=6000
mpg=bmpg-(bmpg*0.2);
elseif TW>6000 && TW<=8000
mpg=bmpg-(bmpg*0.4);
elseif TW>8000 && TW<10000
mpg=bmpg-(bmpg*0.6);
elseif TW>10000
fprintf('exceedes truck weight limit ');
end
case'no'
mpg=18;
end
end
end
fprintf('Miles per Gallon')
disp(mpg)
G=(M/mpg);%%how many gallons used
fprintf('How many Gallons of fuel used')
disp(G)
CF=G*c;%%cost to fuel up
fprintf('Cost to Fuel up')
disp(CF)
CM=(G/M)*c;%%cost per mile
fprintf('Cost per mile')
disp(CM)
x=CF:1:CM;
y=bmpg:1:mpg;
%GUI Program
classdef JonPagelsProjectGUI < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
GridLayout matlab.ui.container.GridLayout
LeftPanel matlab.ui.container.Panel
EditField matlab.ui.control.NumericEditField
NumberofEngineCylindersButtonGroup matlab.ui.container.ButtonGroup
Button_8 matlab.ui.control.RadioButton
Button_6 matlab.ui.control.RadioButton
Button_4 matlab.ui.control.RadioButton
TrailerTowedButtonGroup matlab.ui.container.ButtonGroup
NoButton matlab.ui.control.RadioButton
YesButton matlab.ui.control.RadioButton
FuelTypeButtonGroup matlab.ui.container.ButtonGroup
DieselButton matlab.ui.control.RadioButton
GasolineButton matlab.ui.control.RadioButton
TripinmilesEditField matlab.ui.control.NumericEditField
TripinmilesEditFieldLabel matlab.ui.control.Label
TrailerWeightEditField matlab.ui.control.NumericEditField
TrailerWeightEditFieldLabel matlab.ui.control.Label
costpergallonSlider matlab.ui.control.Slider
costpergallonSliderLabel matlab.ui.control.Label
CenterPanel matlab.ui.container.Panel
PlotButton matlab.ui.control.Button
CalculateButton matlab.ui.control.Button
RightPanel matlab.ui.container.Panel
costmileEditField matlab.ui.control.NumericEditField
costmileEditFieldLabel matlab.ui.control.Label
CosttofuelupEditField matlab.ui.control.NumericEditField
CosttofuelupEditFieldLabel matlab.ui.control.Label
GallonsofusedfuelEditField matlab.ui.control.NumericEditField
GallonsofusedfuelEditFieldLabel matlab.ui.control.Label
MilesGallonEditField matlab.ui.control.NumericEditField
MilesGallonEditFieldLabel matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
% Properties that correspond to apps with auto-reflow
properties (Access = private)
onePanelWidth = 576;
twoPanelWidth = 768;
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: costpergallonSlider
function costpergallonSliderValueChanged(app, event)
c = app.costpergallonSlider.Value;
app.EditField.Value=c;
end
% Selection changed function: FuelTypeButtonGroup
function FuelTypeButtonGroupSelectionChanged(app, event)
F=app.FuelTypeButtonGroup.SelectedObject;
switch F
case app.GasolineButton
case app.DieselButton
end
end
% Selection changed function: NumberofEngineCylindersButtonGroup
function NumberofEngineCylindersButtonGroupSelectionChanged(app, event)
C = app.NumberofEngineCylindersButtonGroup.SelectedObject;
switch C
case app.Button_4
case app.Button_6
case app.Button_8
end
end
% Value changed function: TrailerWeightEditField
function TrailerWeightEditFieldValueChanged(app, event)
TW = app.TrailerWeightEditField.Value;
end
% Value changed function: TripinmilesEditField
function TripinmilesEditFieldValueChanged(app, event)
M = app.TripinmilesEditField.Value;
end
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
M = app.TripinmilesEditField.Value;
TW = app.TrailerWeightEditField.Value;
F=app.FuelTypeButtonGroup.SelectedObject;
T = app.TrailerTowedButtonGroup.SelectedObject;
switch F
case 'gasoline'
case C==4
bmpg=24;
switch T
case 'yes'
if TW<=2000
mpg=bmpg-(bmpg*0.2);
elseif TW>2000
fprintf('exceedes truck weight limit ');
end
case 'no'
mpg=24;
end
case C==6
bmpg=18;
switch T
case 'yes'
if TW<=2000
mpg=bmpg-(bmpg*0.1);
elseif TW>2000 && TW<=4000
mpg=bmpg-(bmpg*0.2);
elseif TW>4000
fprintf('exceedes truck weight limit ');
end
case 'no'
mpg=18;
end
case C==8
bmpg=12;
switch T
case'yes'
if TW<=2000
mpg=bmpg-(bmpg*0.05);
elseif TW>2000 && TW<=4000
mpg=bmpg-(bmpg*0.1);
elseif TW>4000 && TW<=6000
mpg=bmpg-(bmpg*0.2);
elseif TW>6000 && TW<=8000
mpg=bmpg-(bmpg*0.4);
elseif TW>8000
fprintf('exceedes truck weight limit ');
end
case'no'
mpg=12;
end
end
case 'diesel'
case C==4
bmpg=26;
switch T
case'yes'
if TW<=2000
mpg=bmpg-(bmpg*0.05);
elseif TW>2000 && TW<=4000
mpg=bmpg-(bmpg*0.1);
elseif TW>4000
fprintf('exceedes truck weight limit ');
end
case 'no'
mpg=26;
end
case C==6
bmpg=22;
switch T
case'yes'
if TW<=2000
mpg=bmpg-(bmpg*0.025);
elseif TW>2000 && TW<=4000
mpg=bmpg-(mbpg*0.5);
elseif TW>4000 && TW<=6000
mpg=bmpg-(bmpg*0.1);
elseif TW>6000 && TW<=8000
mpg=bmpg-(bmpg*0.2);
elseif TW>8000
fprintf('exceedes truck weight limit ');
end
case'no'
mpg=22;
end
case C==8
bmpg=18;
switch T
case'yes'
if TW<=2000
mpg=bmpg-(bmpg*0.0125);
elseif TW>2000 && TW<=4000
mpg=bmpg-(bmpg*0.05);
elseif TW>4000 && TW<=6000
mpg=bmpg-(bmpg*0.2);
elseif TW>6000 && TW<=8000
mpg=bmpg-(bmpg*0.4);
elseif TW>8000 && TW<10000
mpg=bmpg-(bmpg*0.6);
elseif TW>10000
fprintf('exceedes truck weight limit ');
end
case'no'
mpg=18;
end
end
end
app.MilesGallonEditField.Value=mpg;
app.GallonsofusedfuelEditField.Value=G;
app.costmileEditField.Value=CM;
app.CosttofuelupEditField.Value=CF;
end
% Selection changed function: TrailerTowedButtonGroup
function TrailerTowedButtonGroupSelectionChanged(app, event)
T = app.TrailerTowedButtonGroup.SelectedObject;
switch T
case app.YesButton
case app.NoButton
end
end
% Value changed function: MilesGallonEditField
function MilesGallonEditFieldValueChanged(app, event)
app.MilesGallonEditField.Value=mpg;
end
% Value changed function: GallonsofusedfuelEditField
function GallonsofusedfuelEditFieldValueChanged(app, event)
app.GallonsofusedfuelEditField.Value=G;
end
% Value changed function: CosttofuelupEditField
function CosttofuelupEditFieldValueChanged(app, event)
app.CosttofuelupEditField.Value=CF;
end
% Value changed function: costmileEditField
function costmileEditFieldValueChanged(app, event)
app.costmileEditField.Value=CM;
end
% Changes arrangement of the app based on UIFigure width
function updateAppLayout(app, event)
currentFigureWidth = app.UIFigure.Position(3);
if(currentFigureWidth <= app.onePanelWidth)
% Change to a 3x1 grid
app.GridLayout.RowHeight = {517, 517, 517};
app.GridLayout.ColumnWidth = {'1x'};
app.CenterPanel.Layout.Row = 1;
app.CenterPanel.Layout.Column = 1;
app.LeftPanel.Layout.Row = 2;
app.LeftPanel.Layout.Column = 1;
app.RightPanel.Layout.Row = 3;
app.RightPanel.Layout.Column = 1;
elseif (currentFigureWidth > app.onePanelWidth && currentFigureWidth <= app.twoPanelWidth)
% Change to a 2x2 grid
app.GridLayout.RowHeight = {517, 517};
app.GridLayout.ColumnWidth = {'1x', '1x'};
app.CenterPanel.Layout.Row = 1;
app.CenterPanel.Layout.Column = [1,2];
app.LeftPanel.Layout.Row = 2;
app.LeftPanel.Layout.Column = 1;
app.RightPanel.Layout.Row = 2;
app.RightPanel.Layout.Column = 2;
else
% Change to a 1x3 grid
app.GridLayout.RowHeight = {'1x'};
app.GridLayout.ColumnWidth = {475, '1x', 378};
app.LeftPanel.Layout.Row = 1;
app.LeftPanel.Layout.Column = 1;
app.CenterPanel.Layout.Row = 1;
app.CenterPanel.Layout.Column = 2;
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 3;
end
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.AutoResizeChildren = 'off';
app.UIFigure.Position = [100 100 970 517];
app.UIFigure.Name = 'MATLAB App';
app.UIFigure.SizeChangedFcn = createCallbackFcn(app, @updateAppLayout, true);
% Create GridLayout
app.GridLayout = uigridlayout(app.UIFigure);
app.GridLayout.ColumnWidth = {475, '1x', 378};
app.GridLayout.RowHeight = {'1x'};
app.GridLayout.ColumnSpacing = 0;
app.GridLayout.RowSpacing = 0;
app.GridLayout.Padding = [0 0 0 0];
app.GridLayout.Scrollable = 'on';
% Create LeftPanel
app.LeftPanel = uipanel(app.GridLayout);
app.LeftPanel.ForegroundColor = [1 0 0];
app.LeftPanel.BackgroundColor = [0.8 0.8 0.8];
app.LeftPanel.Layout.Row = 1;
app.LeftPanel.Layout.Column = 1;
% Create costpergallonSliderLabel
app.costpergallonSliderLabel = uilabel(app.LeftPanel);
app.costpergallonSliderLabel.HorizontalAlignment = 'right';
app.costpergallonSliderLabel.FontColor = [1 0.4118 0.1608];
app.costpergallonSliderLabel.Position = [5 39 83 22];
app.costpergallonSliderLabel.Text = 'cost per gallon';
% Create costpergallonSlider
app.costpergallonSlider = uislider(app.LeftPanel);
app.costpergallonSlider.Limits = [2 7.5];
app.costpergallonSlider.MajorTicks = [2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5];
app.costpergallonSlider.ValueChangedFcn = createCallbackFcn(app, @costpergallonSliderValueChanged, true);
app.costpergallonSlider.FontColor = [1 0.4118 0.1608];
app.costpergallonSlider.Position = [109 48 352 3];
app.costpergallonSlider.Value = 2;
% Create TrailerWeightEditFieldLabel
app.TrailerWeightEditFieldLabel = uilabel(app.LeftPanel);
app.TrailerWeightEditFieldLabel.HorizontalAlignment = 'right';
app.TrailerWeightEditFieldLabel.FontColor = [0.6353 0.0784 0.1843];
app.TrailerWeightEditFieldLabel.Position = [5 176 79 22];
app.TrailerWeightEditFieldLabel.Text = 'Trailer Weight';
% Create TrailerWeightEditField
app.TrailerWeightEditField = uieditfield(app.LeftPanel, 'numeric');
app.TrailerWeightEditField.Limits = [0 12000];
app.TrailerWeightEditField.ValueDisplayFormat = '%.0f';
app.TrailerWeightEditField.ValueChangedFcn = createCallbackFcn(app, @TrailerWeightEditFieldValueChanged, true);
app.TrailerWeightEditField.FontColor = [0.6353 0.0784 0.1843];
app.TrailerWeightEditField.Position = [99 176 100 22];
% Create TripinmilesEditFieldLabel
app.TripinmilesEditFieldLabel = uilabel(app.LeftPanel);
app.TripinmilesEditFieldLabel.HorizontalAlignment = 'right';
app.TripinmilesEditFieldLabel.FontColor = [0 0.4471 0.7412];
app.TripinmilesEditFieldLabel.Position = [268 176 69 22];
app.TripinmilesEditFieldLabel.Text = 'Trip in miles';
% Create TripinmilesEditField
app.TripinmilesEditField = uieditfield(app.LeftPanel, 'numeric');
app.TripinmilesEditField.ValueChangedFcn = createCallbackFcn(app, @TripinmilesEditFieldValueChanged, true);
app.TripinmilesEditField.FontColor = [0 0.4471 0.7412];
app.TripinmilesEditField.Position = [352 176 100 22];
% Create FuelTypeButtonGroup
app.FuelTypeButtonGroup = uibuttongroup(app.LeftPanel);
app.FuelTypeButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @FuelTypeButtonGroupSelectionChanged, true);
app.FuelTypeButtonGroup.Title = 'Fuel Type';
app.FuelTypeButtonGroup.BackgroundColor = [1 0 0];
app.FuelTypeButtonGroup.Position = [40 405 123 76];
% Create GasolineButton
app.GasolineButton = uiradiobutton(app.FuelTypeButtonGroup);
app.GasolineButton.Text = 'Gasoline';
app.GasolineButton.Position = [11 30 69 22];
app.GasolineButton.Value = true;
% Create DieselButton
app.DieselButton = uiradiobutton(app.FuelTypeButtonGroup);
app.DieselButton.Text = 'Diesel';
app.DieselButton.Position = [11 8 65 22];
% Create TrailerTowedButtonGroup
app.TrailerTowedButtonGroup = uibuttongroup(app.LeftPanel);
app.TrailerTowedButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @TrailerTowedButtonGroupSelectionChanged, true);
app.TrailerTowedButtonGroup.Title = 'Trailer Towed';
app.TrailerTowedButtonGroup.BackgroundColor = [1 1 0];
app.TrailerTowedButtonGroup.Position = [285 405 123 76];
% Create YesButton
app.YesButton = uiradiobutton(app.TrailerTowedButtonGroup);
app.YesButton.Text = 'Yes';
app.YesButton.Position = [11 30 58 22];
app.YesButton.Value = true;
% Create NoButton
app.NoButton = uiradiobutton(app.TrailerTowedButtonGroup);
app.NoButton.Text = 'No';
app.NoButton.Position = [11 8 65 22];
% Create NumberofEngineCylindersButtonGroup
app.NumberofEngineCylindersButtonGroup = uibuttongroup(app.LeftPanel);
app.NumberofEngineCylindersButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @NumberofEngineCylindersButtonGroupSelectionChanged, true);
app.NumberofEngineCylindersButtonGroup.Title = 'Number of Engine Cylinders';
app.NumberofEngineCylindersButtonGroup.BackgroundColor = [0.6353 0.0784 0.1843];
app.NumberofEngineCylindersButtonGroup.Position = [143 248 167 106];
% Create Button_4
app.Button_4 = uiradiobutton(app.NumberofEngineCylindersButtonGroup);
app.Button_4.Text = '4';
app.Button_4.Position = [11 60 58 22];
app.Button_4.Value = true;
% Create Button_6
app.Button_6 = uiradiobutton(app.NumberofEngineCylindersButtonGroup);
app.Button_6.Text = '6';
app.Button_6.Position = [11 38 65 22];
% Create Button_8
app.Button_8 = uiradiobutton(app.NumberofEngineCylindersButtonGroup);
app.Button_8.Text = '8';
app.Button_8.Position = [11 16 65 22];
% Create EditField
app.EditField = uieditfield(app.LeftPanel, 'numeric');
app.EditField.Position = [186 72 167 22];
% Create CenterPanel
app.CenterPanel = uipanel(app.GridLayout);
app.CenterPanel.Layout.Row = 1;
app.CenterPanel.Layout.Column = 2;
% Create CalculateButton
app.CalculateButton = uibutton(app.CenterPanel, 'push');
app.CalculateButton.ButtonPushedFcn = createCallbackFcn(app, @CalculateButtonPushed, true);
app.CalculateButton.BackgroundColor = [1 0.0745 0.651];
app.CalculateButton.Position = [8 383 100 23];
app.CalculateButton.Text = 'Calculate';
% Create PlotButton
app.PlotButton = uibutton(app.CenterPanel, 'push');
app.PlotButton.Position = [8 136 100 23];
app.PlotButton.Text = 'Plot';
% Create RightPanel
app.RightPanel = uipanel(app.GridLayout);
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 3;
% Create UIAxes
app.UIAxes = uiaxes(app.RightPanel);
title(app.UIAxes, 'mpg vs. cost per mile')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [6 54 316 195];
% Create MilesGallonEditFieldLabel
app.MilesGallonEditFieldLabel = uilabel(app.RightPanel);
app.MilesGallonEditFieldLabel.BackgroundColor = [0 0 1];
app.MilesGallonEditFieldLabel.HorizontalAlignment = 'right';
app.MilesGallonEditFieldLabel.Position = [96 468 74 22];
app.MilesGallonEditFieldLabel.Text = 'Miles /Gallon';
% Create MilesGallonEditField
app.MilesGallonEditField = uieditfield(app.RightPanel, 'numeric');
app.MilesGallonEditField.ValueChangedFcn = createCallbackFcn(app, @MilesGallonEditFieldValueChanged, true);
app.MilesGallonEditField.BackgroundColor = [0 0 1];
app.MilesGallonEditField.Position = [185 468 100 22];
% Create GallonsofusedfuelEditFieldLabel
app.GallonsofusedfuelEditFieldLabel = uilabel(app.RightPanel);
app.GallonsofusedfuelEditFieldLabel.BackgroundColor = [1 0.4118 0.1608];
app.GallonsofusedfuelEditFieldLabel.HorizontalAlignment = 'right';
app.GallonsofusedfuelEditFieldLabel.Position = [96 413 111 22];
app.GallonsofusedfuelEditFieldLabel.Text = 'Gallons of used fuel';
% Create GallonsofusedfuelEditField
app.GallonsofusedfuelEditField = uieditfield(app.RightPanel, 'numeric');
app.GallonsofusedfuelEditField.ValueChangedFcn = createCallbackFcn(app, @GallonsofusedfuelEditFieldValueChanged, true);
app.GallonsofusedfuelEditField.BackgroundColor = [1 0.4118 0.1608];
app.GallonsofusedfuelEditField.Position = [222 413 100 22];
% Create CosttofuelupEditFieldLabel
app.CosttofuelupEditFieldLabel = uilabel(app.RightPanel);
app.CosttofuelupEditFieldLabel.BackgroundColor = [0 1 0];
app.CosttofuelupEditFieldLabel.HorizontalAlignment = 'right';
app.CosttofuelupEditFieldLabel.Position = [96 362 82 22];
app.CosttofuelupEditFieldLabel.Text = 'Cost to fuel up';
% Create CosttofuelupEditField
app.CosttofuelupEditField = uieditfield(app.RightPanel, 'numeric');
app.CosttofuelupEditField.ValueChangedFcn = createCallbackFcn(app, @CosttofuelupEditFieldValueChanged, true);
app.CosttofuelupEditField.BackgroundColor = [0 1 0];
app.CosttofuelupEditField.Position = [193 362 100 22];
% Create costmileEditFieldLabel
app.costmileEditFieldLabel = uilabel(app.RightPanel);
app.costmileEditFieldLabel.BackgroundColor = [0.4941 0.1843 0.5569];
app.costmileEditFieldLabel.HorizontalAlignment = 'right';
app.costmileEditFieldLabel.Position = [96 307 52 22];
app.costmileEditFieldLabel.Text = 'cost/mile';
% Create costmileEditField
app.costmileEditField = uieditfield(app.RightPanel, 'numeric');
app.costmileEditField.ValueChangedFcn = createCallbackFcn(app, @costmileEditFieldValueChanged, true);
app.costmileEditField.BackgroundColor = [0.4941 0.1843 0.5569];
app.costmileEditField.Position = [163 307 100 22];
% 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 = JonPagelsProjectGUI
% 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 个)

Divyanshu
Divyanshu 2023-6-16
Hi Jon,
Few points to consider for better understanding of the sample application as well as the function ‘logic’ written in MATLAB:
  • The logic function accepts 3 arguments: ‘type’, ‘towed’ & ‘eng_num (number of engines).
  • I have just used if-else statements and sample logics in the function which you can modify with switch-case and other formulas depending upon the use-case.
  • Function returns the result which is obtained after some calculation based on 3 input parameters.
Here is the function:
function res = logic(type,towed,eng_num)
if(type == "Gasoline")
if(towed == 1)
res = 4.5*eng_num;
else
res = 2.5*eng_num;
end
else
if(towed == 1)
res = 4.5*eng_num + 1;
else
res = 2.5*eng_num + 1;
end
end
end
  • In the app instead of radio-buttons 3 edit fields can be used for simplicity. And based on the values of these fields call the ‘logic’ function.
Here is the code-view of the app:
properties (Access = private)
ftype % Description
isTowed % Description
eng_num % Description
result
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: CalculateButton
function CalculateBtnPushed(app, event)
app.ftype = app.FuelTypeEditField.Value;
app.isTowed = app.TowedEditField.Value;
app.eng_num = app.NumberofEnginesEditField.Value;
app.result = logic(app.ftype,app.isTowed,app.eng_num);
app.ResultEditField.Value = num2str(app.result);
end
end
Please refer the following documentation for more details on how to use radio buttons and corresponding callbacks in app:

类别

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

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by