gpt4 book ai didi

MATLAB App - 在创建组件之前添加路径

转载 作者:太空宇宙 更新时间:2023-11-03 19:43:11 25 4
gpt4 key购买 nike

我在某个文件夹 ~/myapp/ 中有一个 MATLAB 应用程序 App.mlapp。它使用的函数和 GUI 中使用的一些图形在 ~/myapp/subfolder 中。为了正确运行 App.mlapp,我每次都必须在启动应用程序之前手动将 ~/myapp/subfolder 添加到我的路径中。

如何自动添加子文件夹?

我尝试将 addpath(genpath(~/myapp/subfolder)); 放在 StartupFcn 的开头。但是,由于 StartupFcn 是在组件创建后调用的,这已经需要 ~/myapp/subfolder 中的一些图形,所以这种方法不起作用。这些组件是使用自动创建的函数 createComponents 创建的,无法使用 App Designer 编辑器对其进行编辑。

excaza 要求的最小示例。要创建它,请打开应用程序设计器,创建一个新应用程序,在设计 View 中添加一个按钮并在路径中指定一个图标,路径为按钮属性 -> 文本和图标 -> 更多属性 -> 图标文件。然后从路径中删除图标的目录并尝试运行该应用程序。

classdef app1 < matlab.apps.AppBase

% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Button matlab.ui.control.Button
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';

% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.Icon = 'help_icon.png';
app.Button.Position = [230 321 100 22];
end
end

methods (Access = public)

% Construct app
function app = app1

% Create and configure 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

最佳答案

虽然我理解 MATLAB 决定在 appdesigner 中设计时锁定大量 GUI 代码,但我也一直直言不讳地告诉他们潜在的重大缺点,例如这个缺点。

除了 Soapbox,您可以通过利用 MATLAB 的 class property specification 来解决这个问题行为,在执行类的其余代码之前将属性初始化为其默认属性。

在这种情况下,我们可以添加一个虚拟私有(private)变量并将其设置为 addpath 的输出:

properties (Access = private)
oldpath = addpath('./icons')
end

在传递适当的路径时提供所需的行为。

关于MATLAB App - 在创建组件之前添加路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51189516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com