MATLAB App-在创建组件之前添加路径 [英] MATLAB App - Add path before component creation
问题描述
我在某个文件夹~/myapp/
中有一个MATLAB App App.mlapp .它使用的功能以及GUI中使用的某些图形在~/myapp/subfolder
中.为了正确运行 App.mlapp ,我每次必须在启动应用之前将~/myapp/subfolder
手动添加到我的路径中.
如何自动添加子文件夹?
我尝试将addpath(genpath(~/myapp/subfolder));
放在StartupFcn
的开头.但是,由于StartupFcn
是在创建组件后调用的,而该组件已经需要~/myapp/subfolder
中的某些图形,因此此方法不起作用.组件是使用自动创建的功能createComponents
创建的,无法使用App Designer编辑器进行编辑.
最小的示例,按excaza的要求.要创建它,请打开应用设计器",创建一个新应用,在设计视图"中添加一个按钮,并使用 Button Properties-> Text&在路径中指定一个图标.图标->更多属性->图标文件.然后从路径中删除图标的目录,然后尝试运行该应用程序.
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代码的决定,但我也一直在向他们大声疾呼它们的潜力像这样的重大缺点.
在这种情况下,我们可以添加一个虚拟私有变量并将其设置为 解决方案
While I understand MATLAB's decision to lock away a lot of the GUI's code when designing in appdesigner
, I've also been fairly vocal to them about the potential significant downsides, like this one.
Soapbox aside, you can get around this by exploiting MATLAB's class property specification behavior, which initializes properties to their default properties before the rest of the class' code is executed.
In this case, we can add a dummy private variable and set it to the output of addpath
:
properties (Access = private)
oldpath = addpath('./icons')
end
Which provides the desired behavior when passed the appropriate path.
这篇关于MATLAB App-在创建组件之前添加路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!