matlab app designer里用List Box 和Button实现从四种函数中选择一个函数进行plot命令绘图,可以主动切换方法,然后点击button出图象,List Box用switch的方法.需要list box和button的所有代码
时间: 2024-05-16 11:16:50 浏览: 113
以下是一个简单的例子,演示如何使用List Box和Button在MATLAB App Designer中实现从四种函数中选择一个函数进行绘图,并可以通过点击按钮进行绘图。
首先,创建一个新的App Designer应用程序,并在UI FIGURE中添加一个List Box和一个Button。
接下来,为List Box添加四个选项,分别是sin、cos、tan和exp。可以在App Designer的“回调函数”选项卡中添加以下代码:
```matlab
function ListBoxValueChanged(app, event)
% 获取ListBox当前选中的值
selectedValue = app.ListBox.Value;
% 根据选择的不同函数进行绘图
switch selectedValue
case 'sin'
plot(app.UIAxes, sin(-pi:0.1:pi));
case 'cos'
plot(app.UIAxes, cos(-pi:0.1:pi));
case 'tan'
plot(app.UIAxes, tan(-pi:0.1:pi));
case 'exp'
plot(app.UIAxes, exp(-pi:0.1:pi));
end
```
这段代码将在用户选择ListBox中的选项时被调用,并根据所选函数进行绘图。
然后,为Button添加一个回调函数,以便在用户点击按钮时执行绘图命令。可以在App Designer的“回调函数”选项卡中添加以下代码:
```matlab
function PlotButtonPushed(app, event)
% 获取ListBox当前选中的值
selectedValue = app.ListBox.Value;
% 根据选择的不同函数进行绘图
switch selectedValue
case 'sin'
plot(app.UIAxes, sin(-pi:0.1:pi));
case 'cos'
plot(app.UIAxes, cos(-pi:0.1:pi));
case 'tan'
plot(app.UIAxes, tan(-pi:0.1:pi));
case 'exp'
plot(app.UIAxes, exp(-pi:0.1:pi));
end
```
这段代码将在用户点击按钮时被调用,并根据所选函数进行绘图。
最后,为了使界面更加友好,可以在ListBox的“属性”选项卡中将“Items”属性设置为“{'sin', 'cos', 'tan', 'exp'}”,以便在ListBox中显示四个选项。
至此,List Box和Button的所有代码就完成了。完整的App Designer应用程序代码如下:
```matlab
classdef MyApp < matlab.apps.AppBase
% App properties
properties (Access = public)
UIFigure matlab.ui.Figure
UIAxes matlab.ui.control.UIAxes
ListBox matlab.ui.control.ListBox
PlotButton matlab.ui.control.Button
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: ListBox
function ListBoxValueChanged(app, event)
% 获取ListBox当前选中的值
selectedValue = app.ListBox.Value;
% 根据选择的不同函数进行绘图
switch selectedValue
case 'sin'
plot(app.UIAxes, sin(-pi:0.1:pi));
case 'cos'
plot(app.UIAxes, cos(-pi:0.1:pi));
case 'tan'
plot(app.UIAxes, tan(-pi:0.1:pi));
case 'exp'
plot(app.UIAxes, exp(-pi:0.1:pi));
end
end
% Button pushed function: PlotButton
function PlotButtonPushed(app, event)
% 获取ListBox当前选中的值
selectedValue = app.ListBox.Value;
% 根据选择的不同函数进行绘图
switch selectedValue
case 'sin'
plot(app.UIAxes, sin(-pi:0.1:pi));
case 'cos'
plot(app.UIAxes, cos(-pi:0.1:pi));
case 'tan'
plot(app.UIAxes, tan(-pi:0.1:pi));
case 'exp'
plot(app.UIAxes, exp(-pi:0.1:pi));
end
end
end
% App initialization and construction
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 = 'My App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Plot')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
app.UIAxes.Position = [150 100 300 300];
% Create ListBox
app.ListBox = uilistbox(app.UIFigure);
app.ListBox.Items = {'sin', 'cos', 'tan', 'exp'};
app.ListBox.ValueChangedFcn = createCallbackFcn(app, @ListBoxValueChanged, true);
app.ListBox.Position = [50 250 75 100];
% Create PlotButton
app.PlotButton = uibutton(app.UIFigure, 'push');
app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
app.PlotButton.Position = [50 200 75 25];
app.PlotButton.Text = 'Plot';
% 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 = MyApp
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
% Show the figure
if nargout == 0
clear app
return
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
```
阅读全文