【发布时间】:2018-07-05 10:54:18
【问题描述】:
我在某个文件夹 ~/myapp/ 中有一个 MATLAB 应用程序App.mlapp。它使用的功能和 GUI 中使用的一些图形在~/myapp/subfolder 中。为了正确运行 App.mlapp,我必须每次在启动应用程序之前手动将 ~/myapp/subfolder 添加到我的路径中。
如何自动添加子文件夹?
我尝试将addpath(genpath(~/myapp/subfolder)); 放在StartupFcn 的开头。然而,由于StartupFcn在组件创建后被调用,这已经需要~/myapp/subfolder中的一些图形,这种方法不起作用。组件是使用自动创建的函数createComponents 创建的,无法使用 App Designer Editor 进行编辑。
excaza 要求的最小示例。要创建它,请打开 App Designer,创建一个新应用,在设计视图中添加一个按钮,并在路径中指定一个图标,Button Properties -> Text & Icon -> More Properties -> Icon File。然后从路径中删除图标的目录并尝试运行应用程序。
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
【问题讨论】:
-
在加载图像之前不能使用
addpath吗?或cd到您的应用程序目录并使用相对路径?请参阅minimal reproducible example。 -
@excaza 我无法编辑
createComponents函数,该函数会加载自定义图标(例如 app.Help.Icon = 'help_icon.png')。 MATLAB 不接受不在路径中的图标:我已经尝试在 App Designer 设计视图中将图标文件指定为“./subfolder/help_icon.png”。
标签: matlab matlab-app-designer