How to change name of UIAxes plot when using a button?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
August
am 2 Mai 2023
Beantwortet: Walter Roberson
am 2 Mai 2023
I am currently making an app that has a drop down with sinx,cosx, and tanx. One of the three options are selected and then a button is pushed to get the derivative and then it will graph whatever was chosen in the drop down. I would like to make my app change the title to 'Derivitave of sinx' if sinx was chosen from the drop down or 'Derivative of cosx' if cosx was chosen from the drop down. I will attach my code below and design below.

classdef DerivativeCalculator < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
derivative matlab.ui.control.Label
Button matlab.ui.control.Button
DerivativeCalculatorGraphLabel matlab.ui.control.Label
DropDown matlab.ui.control.DropDown
Label matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
method = app.DropDown.Value
if method == 'Sinx'
result = 'cosx';
elseif method == 'Cosx'
result = '-sinx';
elseif method == 'Tanx'
result = 'sec^2(x)';
elseif method == 'Cotx'
result = '-csc^2(x)';
elseif method == 'Secx'
result = 'secxtanx';
elseif method == 'Cscx'
result = '-cscxcotx';
end
% converting result to 2 decimal numeric string
output = result;
% setting string to label field
app.derivative.Text = output;
% displaying changes
drawnow;
switch method
case 'Sinx'
result = 'cosx';
x = -pi:0.01:pi;
plot(app.UIAxes,x,sin(x))
case 'Cosx';
result = '-sinx';
x = -pi:0.01:pi;
plot(app.UIAxes,x,cos(x))
case 'Tanx';
result = 'sec^2x';
x = (-pi/2)+0.01:0.01:(pi/2)-0.01;
plot(app.UIAxes,x,tan(x))
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.Position = [100 100 640 480];
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.Position = [147 38 358 216];
% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.BackgroundColor = [1 1 1];
app.Label.HorizontalAlignment = 'center';
app.Label.FontSize = 18;
app.Label.FontWeight = 'bold';
app.Label.Position = [148 293 42 23];
app.Label.Text = 'd/dx';
% Create DropDown
app.DropDown = uidropdown(app.UIFigure);
app.DropDown.Items = {'Sinx', 'Cosx', 'Tanx', 'Cotx', 'Secx', 'Cscx'};
app.DropDown.Position = [197 293 100 22];
app.DropDown.Value = 'Sinx';
% Create DerivativeCalculatorGraphLabel
app.DerivativeCalculatorGraphLabel = uilabel(app.UIFigure);
app.DerivativeCalculatorGraphLabel.BackgroundColor = [1 1 1];
app.DerivativeCalculatorGraphLabel.HorizontalAlignment = 'center';
app.DerivativeCalculatorGraphLabel.Position = [245 406 164 22];
app.DerivativeCalculatorGraphLabel.Text = 'Derivative Calculator & Graph';
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Position = [329 293 47 23];
app.Button.Text = '=';
% Create derivative
app.derivative = uilabel(app.UIFigure);
app.derivative.BackgroundColor = [1 1 1];
app.derivative.HorizontalAlignment = 'center';
app.derivative.Position = [415 293 89 22];
app.derivative.Text = '';
% 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 = DerivativeCalculator
% 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 Kommentare
Akzeptierte Antwort
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Develop Apps Using App Designer finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!