我基于具有多个函数和文件的 gui 创建了一个可执行文件,如果我在安装文件夹中或使用桌面快捷方式打开可执行文件,一切正常。如果我通过开始菜单打开,可执行文件不会合并图像并且不会运行。我可以做些什么来防止此问题?是否可以阻止窗口开始菜单中的快捷方式?
我在这里找到了一个解决方案:
您可以使用以下函数获取已执行exe
文件的文件夹:
function currentDir = getcurrentdir()
if isdeployed % Stand-alone mode.
[status, result] = system('path');
currentDir = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
else % MATLAB mode.
currentDir = pwd;
end
调用该函数并在 GUI 打开函数中使用cd
:
currentDir = getcurrentdir();
cd(currentDir);
我创建了一个guide
测试应用程序,并使用deploytool
进行编译和打包以进行外部部署。
为了进行测试,我在 GUI 中添加了一个文本标签(标签名称:text2
)。
在 GUI 打开函数中,我添加了以下代码:
% --- Executes just before GuideApp is made visible.
function GuideApp_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.text2.String = 'Starting...';
pause(1);
currentDir = getcurrentdir();
%Set the label's text to display currentDir.
handles.text2.String = currentDir;
%Change directory to the directory of the exe file.
cd(currentDir);
%Create a file in the directory (just for testing):
f = fopen('currentDir.txt', 'w');fprintf(f, '%srn', currentDir);fclose(f);
% Update handles structure
guidata(hObject, handles);
上述解决方案工作正常:
标签的文本显示exe文件的路径。currentDir.txt
文件是在 exe 文件的路径中创建的。