MATLAB AppDesigner 设计UI界面中调用自定义函数

在MATLAB AppDesigner设计UI界面过程中,如果直接在APPDesigner代码编辑框中编写代码,如代码量较大,会导致代码混乱的问题。使用调用函数的方法能够解决该问题。
本文将介绍MATLAB AppDesigner 设计UI界面时如何调用函数,函数该如何写,如何使用app对象的成员变量及函数。

函数代码:函数中如需使用app的成员变量或成员函数,则需要传递app参数。

function Environment(app)
	{%函数内容}
end

APPDesigner UI界面中调用函数:

Environment(app);

函数中如何使用app的成员变量:

app.single_node %single_node为app的一个成员变量

函数中如何在app的UI界面的坐标系上面画图:

plot(app.route_graph,[0 638.75],[638.75 638.75],'k-');%route_graph为坐标系变量名
hold(app.route_graph,'on');%等待
  • 6
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
MATLAB App Designer,要定义一个函数,你可以在左侧的“组件”窗格选择一个“函数”组件,然后在右侧的“属性”窗格设置其属性,包括函数名称、输入参数和输出参数等。 接下来,你需要在“函数编辑器”编写函数代码。你可以通过单击左侧的“函数”组件来打开“函数编辑器”,然后在其编写MATLAB代码。在函数编辑器,你可以定义输入和输出参数、编写函数体,并使用MATLAB内置的函数和命令。 例如,以下是一个简单的MATLAB App Designer应用程序,其包含一个名为“myFunction”的函数组件: ``` classdef MyApp < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure MyFunction matlab.ui.container.Function end % Callbacks that handle component events methods (Access = private) % Button pushed function: CallFunctionButton function CallFunctionButtonPushed(app, event) % Call the myFunction component output = app.MyFunction(app.InputEditField.Value); % Display the output in the OutputTextArea app.OutputTextArea.Value = output; 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 InputEditField app.InputEditField = uieditfield(app.UIFigure, 'text'); app.InputEditField.Position = [30 50 100 22]; % Create CallFunctionButton app.CallFunctionButton = uibutton(app.UIFigure, 'push'); app.CallFunctionButton.ButtonPushedFcn = createCallbackFcn(app, @CallFunctionButtonPushed, true); app.CallFunctionButton.Position = [30 100 100 22]; app.CallFunctionButton.Text = 'Call Function'; % Create OutputTextArea app.OutputTextArea = uitextarea(app.UIFigure); app.OutputTextArea.Position = [200 50 200 100]; end end % App initialization and construction methods (Access = public) % Construct app function app = MyApp % Create and configure components createComponents(app) % Register the app with App Designer registerApp(app, app.UIFigure) % Run the startup function runStartupFcn(app, @startupFcn) % Enable/disable the app components as necessary enableDisableComponents(app, app.UIFigure) % Show the figure after all components are created app.UIFigure.Visible = 'on'; end % Code that executes before app deletion function delete(app) % Delete UIFigure when app is deleted delete(app.UIFigure) end end % Component initialization methods (Access = private) % Initialize the app startup function function startupFcn(app) % Initialize the myFunction component app.MyFunction = myFunction; end end end ``` 在上面的代码,我们创建了一个名为“My App”的MATLAB App Designer应用程序,其包含一个名为“myFunction”的函数组件。在应用程序,我们还创建了一个输入编辑字段、一个调用函数的按钮和一个输出文本区域。 在按钮的回调函数,我们调用myFunction组件并传入输入编辑字段的值,然后将输出显示在输出文本区域。 你可以在函数组件的属性窗格设置函数名称、输入参数和输出参数等,并在函数编辑器编写函数代码。注意,在函数组件定义函数只能在应用程序使用,不能在MATLAB命令窗口使用。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jasmine-Lily

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值