How can I plot a user function in AppDesigner?

조회 수: 2 (최근 30일)
Andres David Mestizo Valencia
Andres David Mestizo Valencia 2020년 9월 1일
댓글: Sourabh Kondapaka 2020년 9월 14일
Well, I am developing an application that will allow to graph a signal either discrete or continuous, however, I do not know how I can read the function entered by the user in the text field to be able to graph it, I appreciate any help or advice.
Here I leave my code:
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
calculate matlab.ui.control.Button
FunctionEditFieldLabel matlab.ui.control.Label
functionf matlab.ui.control.EditField
SamplingSliderLabel matlab.ui.control.Label
slider matlab.ui.control.Slider
Graph matlab.ui.control.UIAxes
APPLICATIONPROYECTDISCRETANDCONTINUESIGNALLabel matlab.ui.control.Label
tgswitch matlab.ui.control.Switch
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: calculate
function calculateButtonPushed(app, event)
%1. ASIGNAR VARIABLES A LOS CAMPOS DE ENTRADA
t = -5 : 0.001 : 5;
Fun = app.functionf.Value;
Tipo = app.tgswitch.Value;
Muestreo = app.slider;
%2. DEFINIR VALORES DE SALIDA
if strcmp(Tipo,'Continue')
cla(app.Graph, 'reset');
plot(app.Graph, Fun);
else
if strcmp(Tipo, 'Discret')
cla(app.Graph, 'reset');
stem(app.Graph, Fun);
end
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.Color = [0.9412 0.9412 0.9412];
app.UIFigure.Position = [100 100 727 386];
app.UIFigure.Name = 'UI Figure';
% Create calculate
app.calculate = uibutton(app.UIFigure, 'push');
app.calculate.ButtonPushedFcn = createCallbackFcn(app, @calculateButtonPushed, true);
app.calculate.Position = [289 25 152 22];
app.calculate.Text = 'Calculate';
% Create FunctionEditFieldLabel
app.FunctionEditFieldLabel = uilabel(app.UIFigure);
app.FunctionEditFieldLabel.HorizontalAlignment = 'right';
app.FunctionEditFieldLabel.Position = [14 231 52 22];
app.FunctionEditFieldLabel.Text = 'Function';
% Create functionf
app.functionf = uieditfield(app.UIFigure, 'text');
app.functionf.Position = [81 231 205 22];
% Create tgswitch
app.tgswitch = uiswitch(app.UIFigure, 'slider');
app.tgswitch.Items = {'Continue', 'Discret'};
app.tgswitch.Position = [127 170 45 20];
app.tgswitch.Value = 'Continue';
% Create SamplingSliderLabel
app.SamplingSliderLabel = uilabel(app.UIFigure);
app.SamplingSliderLabel.HorizontalAlignment = 'right';
app.SamplingSliderLabel.Position = [14 115 56 22];
app.SamplingSliderLabel.Text = 'Sampling';
% Create slider
app.slider = uislider(app.UIFigure);
app.slider.Position = [91 124 203 3];
% Create Graph
app.Graph = uiaxes(app.UIFigure);
title(app.Graph, 'Signal')
xlabel(app.Graph, 'X')
ylabel(app.Graph, 'Y')
app.Graph.Position = [304 70 410 227];
% Create APPLICATIONPROYECTDISCRETANDCONTINUESIGNALLabel
app.APPLICATIONPROYECTDISCRETANDCONTINUESIGNALLabel = uilabel(app.UIFigure);
app.APPLICATIONPROYECTDISCRETANDCONTINUESIGNALLabel.HorizontalAlignment = 'center';
app.APPLICATIONPROYECTDISCRETANDCONTINUESIGNALLabel.FontSize = 15;
app.APPLICATIONPROYECTDISCRETANDCONTINUESIGNALLabel.FontWeight = 'bold';
app.APPLICATIONPROYECTDISCRETANDCONTINUESIGNALLabel.Position = [139.5 330 451 22];
app.APPLICATIONPROYECTDISCRETANDCONTINUESIGNALLabel.Text = 'APPLICATION PROYECT (DISCRET AND CONTINUE SIGNAL)';
% 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
  댓글 수: 7
Mario Malic
Mario Malic 2020년 9월 12일
편집: Mario Malic 2020년 9월 12일
You can put your answer in the answers section and accept it yourself, It's better to attach it as a file, since the code is long.
Sourabh Kondapaka
Sourabh Kondapaka 2020년 9월 14일
As @Mario Malic suggested, it would be better if you post your comment as an answer and accept it, as it would be easy for others who find this question.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by