matlab app designer里用List Box 和Button实现从四种函数中选择一个函数进行plot命令绘图,可以主动切换方法,然后点击button出图象,List Box用switch的方法
时间: 2024-05-14 19:19:24 浏览: 132
好的,那我们可以先创建一个 List Box 和一个 Button,然后在 List Box 中添加四种函数的名称,用 switch-case 语句实现根据用户选择的函数名称来选择对应的函数进行绘图。最后在 Button 的回调函数中调用选择的函数进行绘图。
下面是一个示例代码:
```matlab
classdef MyApp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
ListBox matlab.ui.control.ListBox
Button matlab.ui.control.Button
Axes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
% Get the selected function name from ListBox
selectedFunction = app.ListBox.Value;
% Call the corresponding function based on the selected name
switch selectedFunction
case 'sin'
x = linspace(0, 2*pi, 100);
y = sin(x);
plot(app.Axes, x, y);
case 'cos'
x = linspace(0, 2*pi, 100);
y = cos(x);
plot(app.Axes, x, y);
case 'tan'
x = linspace(-pi/2, pi/2, 100);
y = tan(x);
plot(app.Axes, x, y);
case 'exp'
x = linspace(-5, 5, 100);
y = exp(x);
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 = 'My App';
% Create ListBox
app.ListBox = uilistbox(app.UIFigure);
app.ListBox.Items = {'sin', 'cos', 'tan', 'exp'};
app.ListBox.Position = [50 100 100 200];
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Position = [200 150 100 22];
app.Button.Text = 'Plot';
% Create Axes
app.Axes = uiaxes(app.UIFigure);
title(app.Axes, 'Plot');
xlabel(app.Axes, 'X');
ylabel(app.Axes, 'Y');
app.Axes.Position = [350 100 250 200];
end
end
% App lifecycle management
methods (Access = public)
% Construct app
function app = MyApp
% Create and configure components
createComponents(app)
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
end
```
这里我们创建了一个名为 `MyApp` 的 App,并在其中添加了一个 ListBox、一个 Button 和一个 Axes。ListBox 中添加了四种函数名称,Button 的回调函数中根据用户选择的函数名称调用对应的函数进行绘图。最后将绘制的图像显示在 Axes 中。
阅读全文
相关推荐


















