Plotting 5 Data Points and Display it in Graph

5 vues (au cours des 30 derniers jours)
John Mark Enhaynes
John Mark Enhaynes le 10 Mai 2021
We are asked to plot 5 Data Points, however I cannot plot it properly. The attached image is my proposed GUI
I used this code below and now i'm stuck and it doesn't produce the desired result that I need.
classdef Enhaynes_FinalExam < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
y5EditField matlab.ui.control.NumericEditField
y5EditFieldLabel matlab.ui.control.Label
x5EditField matlab.ui.control.NumericEditField
x5EditFieldLabel matlab.ui.control.Label
y4EditField matlab.ui.control.NumericEditField
y4EditFieldLabel matlab.ui.control.Label
x4EditField matlab.ui.control.NumericEditField
x4EditFieldLabel matlab.ui.control.Label
y3EditField matlab.ui.control.NumericEditField
y3EditFieldLabel matlab.ui.control.Label
x3EditField matlab.ui.control.NumericEditField
x3EditFieldLabel matlab.ui.control.Label
y2EditField matlab.ui.control.NumericEditField
y2EditFieldLabel matlab.ui.control.Label
x2EditField matlab.ui.control.NumericEditField
x2EditFieldLabel matlab.ui.control.Label
y1EditField matlab.ui.control.NumericEditField
y1EditFieldLabel matlab.ui.control.Label
x1EditField matlab.ui.control.NumericEditField
x1EditFieldLabel matlab.ui.control.Label
DataPointsLabel matlab.ui.control.Label
PlotwithLogarithmicButton matlab.ui.control.Button
PlotwithParabolicButton matlab.ui.control.Button
PlotwithLinearButton matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: PlotwithLinearButton
function PlotwithLinearButtonPushed(app, event)
x1 = app.x1EditField.Value;
x2 = app.x2EditField.Value;
x3 = app.x3EditField.Value;
x4 = app.x4EditField.Value;
x5 = app.x5EditField.Value;
y1 = app.y1EditField.Value;
y2 = app.y2EditField.Value;
y3 = app.y3EditField.Value;
y4 = app.y4EditField.Value;
y5 = app.y5EditField.Value;
plot(app.UIAxes,[x1 x2 x3 x4 x5],'--x');
plot(app.UIAxes,[y1 y2 y3 y4 y5],'--y');
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 761 503];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Graph Anything!')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [1 198 758 306];
% Create PlotwithLinearButton
app.PlotwithLinearButton = uibutton(app.UIFigure, 'push');
app.PlotwithLinearButton.ButtonPushedFcn = createCallbackFcn(app, @PlotwithLinearButtonPushed, true);
app.PlotwithLinearButton.Position = [132 142 100 22];
app.PlotwithLinearButton.Text = 'Plot with Linear';
% Create PlotwithParabolicButton
app.PlotwithParabolicButton = uibutton(app.UIFigure, 'push');
app.PlotwithParabolicButton.Position = [300 142 115 22];
app.PlotwithParabolicButton.Text = 'Plot with Parabolic';
% Create PlotwithLogarithmicButton
app.PlotwithLogarithmicButton = uibutton(app.UIFigure, 'push');
app.PlotwithLogarithmicButton.Position = [500 142 127 22];
app.PlotwithLogarithmicButton.Text = 'Plot with Logarithmic';
% Create DataPointsLabel
app.DataPointsLabel = uilabel(app.UIFigure);
app.DataPointsLabel.FontWeight = 'bold';
app.DataPointsLabel.Position = [17 54 71 22];
app.DataPointsLabel.Text = 'Data Points';
% Create x1EditFieldLabel
app.x1EditFieldLabel = uilabel(app.UIFigure);
app.x1EditFieldLabel.HorizontalAlignment = 'center';
app.x1EditFieldLabel.Position = [106 75 25 22];
app.x1EditFieldLabel.Text = 'x1';
% Create x1EditField
app.x1EditField = uieditfield(app.UIFigure, 'numeric');
app.x1EditField.HorizontalAlignment = 'center';
app.x1EditField.Position = [146 75 53 22];
% Create y1EditFieldLabel
app.y1EditFieldLabel = uilabel(app.UIFigure);
app.y1EditFieldLabel.HorizontalAlignment = 'center';
app.y1EditFieldLabel.Position = [107 34 25 22];
app.y1EditFieldLabel.Text = 'y1';
% Create y1EditField
app.y1EditField = uieditfield(app.UIFigure, 'numeric');
app.y1EditField.HorizontalAlignment = 'center';
app.y1EditField.Position = [147 34 53 22];
% Create x2EditFieldLabel
app.x2EditFieldLabel = uilabel(app.UIFigure);
app.x2EditFieldLabel.HorizontalAlignment = 'center';
app.x2EditFieldLabel.Position = [208 74 25 22];
app.x2EditFieldLabel.Text = 'x2';
% Create x2EditField
app.x2EditField = uieditfield(app.UIFigure, 'numeric');
app.x2EditField.HorizontalAlignment = 'center';
app.x2EditField.Position = [248 74 53 22];
% Create y2EditFieldLabel
app.y2EditFieldLabel = uilabel(app.UIFigure);
app.y2EditFieldLabel.HorizontalAlignment = 'center';
app.y2EditFieldLabel.Position = [209 33 25 22];
app.y2EditFieldLabel.Text = 'y2';
% Create y2EditField
app.y2EditField = uieditfield(app.UIFigure, 'numeric');
app.y2EditField.HorizontalAlignment = 'center';
app.y2EditField.Position = [249 33 53 22];
% Create x3EditFieldLabel
app.x3EditFieldLabel = uilabel(app.UIFigure);
app.x3EditFieldLabel.HorizontalAlignment = 'center';
app.x3EditFieldLabel.Position = [322 74 25 22];
app.x3EditFieldLabel.Text = 'x3';
% Create x3EditField
app.x3EditField = uieditfield(app.UIFigure, 'numeric');
app.x3EditField.HorizontalAlignment = 'center';
app.x3EditField.Position = [362 74 53 22];
% Create y3EditFieldLabel
app.y3EditFieldLabel = uilabel(app.UIFigure);
app.y3EditFieldLabel.HorizontalAlignment = 'center';
app.y3EditFieldLabel.Position = [323 33 25 22];
app.y3EditFieldLabel.Text = 'y3';
% Create y3EditField
app.y3EditField = uieditfield(app.UIFigure, 'numeric');
app.y3EditField.HorizontalAlignment = 'center';
app.y3EditField.Position = [363 33 53 22];
% Create x4EditFieldLabel
app.x4EditFieldLabel = uilabel(app.UIFigure);
app.x4EditFieldLabel.HorizontalAlignment = 'center';
app.x4EditFieldLabel.Position = [436 74 25 22];
app.x4EditFieldLabel.Text = 'x4';
% Create x4EditField
app.x4EditField = uieditfield(app.UIFigure, 'numeric');
app.x4EditField.HorizontalAlignment = 'center';
app.x4EditField.Position = [476 74 53 22];
% Create y4EditFieldLabel
app.y4EditFieldLabel = uilabel(app.UIFigure);
app.y4EditFieldLabel.HorizontalAlignment = 'center';
app.y4EditFieldLabel.Position = [437 33 25 22];
app.y4EditFieldLabel.Text = 'y4';
% Create y4EditField
app.y4EditField = uieditfield(app.UIFigure, 'numeric');
app.y4EditField.HorizontalAlignment = 'center';
app.y4EditField.Position = [477 33 53 22];
% Create x5EditFieldLabel
app.x5EditFieldLabel = uilabel(app.UIFigure);
app.x5EditFieldLabel.HorizontalAlignment = 'center';
app.x5EditFieldLabel.Position = [549 74 25 22];
app.x5EditFieldLabel.Text = 'x5';
% Create x5EditField
app.x5EditField = uieditfield(app.UIFigure, 'numeric');
app.x5EditField.HorizontalAlignment = 'center';
app.x5EditField.Position = [589 74 53 22];
% Create y5EditFieldLabel
app.y5EditFieldLabel = uilabel(app.UIFigure);
app.y5EditFieldLabel.HorizontalAlignment = 'center';
app.y5EditFieldLabel.Position = [550 33 25 22];
app.y5EditFieldLabel.Text = 'y5';
% Create y5EditField
app.y5EditField = uieditfield(app.UIFigure, 'numeric');
app.y5EditField.HorizontalAlignment = 'center';
app.y5EditField.Position = [590 33 53 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 = Enhaynes_FinalExam
% 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
If you can suggest more practical approach, I'm all ears.

Réponses (1)

Reshma Nerella
Reshma Nerella le 21 Juin 2021
Hi,
The possible reason for the blank UIAxes without plot is that all the 10 Numeric Edit fields have Value 0.
In the callback function PlotwithLinearButtonPushed
plot(app.UIAxes,[x1 x2 x3 x4 x5],'--x');
plot(app.UIAxes,[y1 y2 y3 y4 y5],'--y');
This will plot x,y values independently and since you didn't use 'hold on', only the 2nd line will be visible.
If you want to plot corresponding x values with y values, consider changing it to
plot(app.UIAxes,[x1 x2 x3 x4 x5],[y1,y2,y3,y4,y5]);
Hope this helps!

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by