MATLAB app designer里用单选按钮组绘制三幅不同图像的代码,使用plot命令进行绘制
时间: 2024-05-07 15:21:32 浏览: 160
用菜单和按钮来控制绘图
4星 · 用户满意度95%
以下是一个例子,展示如何使用单选按钮组在MATLAB App Designer中绘制三幅不同的图像:
1. 在App Designer中创建一个新的Blank App
2. 在Design View中添加一个Axes组件和一个Button Group组件
3. 在Axes中绘制一个默认的图像(例如,使用plot命令绘制一个sin函数)
4. 在Button Group中添加三个单选按钮,并设置它们的String属性为“Plot 1”,“Plot 2”和“Plot 3”
5. 在Button Group中添加一个SelectionChangedFcn回调函数,用于检测单选按钮的更改,并在Axes中绘制相应的图像
以下是示例代码:
```matlab
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Axes matlab.ui.control.UIAxes
ButtonGroup matlab.ui.container.ButtonGroup
Plot1Button matlab.ui.control.RadioButton
Plot2Button matlab.ui.control.RadioButton
Plot3Button matlab.ui.control.RadioButton
end
% Callbacks that handle component events
methods (Access = private)
% Button group selection changed function
function ButtonGroupSelectionChanged(app, event)
selectedButton = app.ButtonGroup.SelectedObject;
if selectedButton == app.Plot1Button
% Plot the first graph
x = linspace(0, 2*pi);
y = sin(x);
plot(app.Axes, x, y);
elseif selectedButton == app.Plot2Button
% Plot the second graph
x = linspace(-5, 5);
y = x.^2;
plot(app.Axes, x, y);
elseif selectedButton == app.Plot3Button
% Plot the third graph
x = -5:0.1:5;
y = exp(-x.^2);
plot(app.Axes, x, y);
end
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
app.UIFigure.Resize = 'off';
% Create Axes
app.Axes = uiaxes(app.UIFigure);
title(app.Axes, 'Plot 1');
xlabel(app.Axes, 'X');
ylabel(app.Axes, 'Y');
app.Axes.Position = [150 100 300 300];
% Create ButtonGroup
app.ButtonGroup = uibuttongroup(app.UIFigure);
app.ButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ButtonGroupSelectionChanged, true);
app.ButtonGroup.Title = 'Select Plot';
app.ButtonGroup.Position = [50 100 70 127];
% Create Plot1Button
app.Plot1Button = uiradiobutton(app.ButtonGroup);
app.Plot1Button.Text = 'Plot 1';
app.Plot1Button.Position = [11 87 56 22];
app.Plot1Button.Value = true;
% Create Plot2Button
app.Plot2Button = uiradiobutton(app.ButtonGroup);
app.Plot2Button.Text = 'Plot 2';
app.Plot2Button.Position = [11 64 56 22];
% Create Plot3Button
app.Plot3Button = uiradiobutton(app.ButtonGroup);
app.Plot3Button.Text = 'Plot 3';
app.Plot3Button.Position = [11 41 56 22];
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)
% Execute the startup function
runStartupFcn(app, @startupFcn)
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
```
在这里,我们创建了一个名为app1的类,并在其中定义了三个单选按钮和一个Axes。在单选按钮的SelectionChangedFcn回调函数中,我们使用plot命令绘制相应的图形。示例代码中绘制的三幅图像分别为sin函数、二次函数和高斯函数。
将此代码复制并运行,您应该能够在应用程序中选择三个不同的图像,并在Axes中绘制它们。
阅读全文