setting current directory using cd from inside .mlapp files

조회 수: 9 (최근 30일)
Timothy
Timothy 2025년 7월 2일
댓글: Timothy 2025년 7월 3일
I'm making an app and trying to understand the path behavior for .mlapp files. I'm observing the following behavior:
  • If I open Matlab by double-clicking on an .mlapp file the path is automatically set to the path of the .mlapp file.
  • If Matlab is already open, however, and the current directory does not corerspond to the directory of the .mlapp file, then warnings get thrown when the .mlapp file is opened by double-clicking on it, and errors get thrown when any buttons are pushed because <<some method>> is not defined for the class associated with <<your app's class>>.
  • I tried to fix this by putting the following code into the startup function:
cd(fileparts(mfilename('fullpath')));
The above, however did not work. If Matlab is not already set to the correct directory, then running the .mlapp file by double-clicking on it does not result in the current directory being updated despite the new code in the startup function. However, if modify to the following, I see the correct path printed on the commandline!
pth = fileparts(mfilename('fullpath'));
disp(pth);
cd(pth)
This means that the code is being executed at some level, and that the variable "pth" has the correct path, but somehow the cd() command executed in the startup function is not actually changing Matlab's current directory and callbacks etc. still throw errors.
The behavior gets further modified if the appdesigner gets involved:
  • If I open appdesigner and I run the app via the "run" button in appdesigner, then it does work, sort of: the path doesn't update in the command window but I don't get crashes and if I do any subsequent path changes via callbacks then the does update in the command line.
  • If I put a break point inside the code and step through it, then behavior is exactly as expected, the Matlab command-line updates as expected, it's basically as if a standard m-file is being executed.
  • If I change the path manually to some other path and run the .mlapp app via double-clicking on it, now it works as opposed to throwing an error, like it did before I opened appdesigner.
I've passed my app around to some others for testing and they are not as familiar with Matlab. I am quite sure what will happen is that: 1) they will open Matlab in whatever directory it was previously set to, then 2) they will double click on the .mlapp file and 3) they will get errors because it is not auto-setting the path like it seems that it should in the startup function.
Is there a better way to set the path from within a .mlapp file, so that when it gets executed by double-clicking the path gets updated?
  댓글 수: 3
Timothy
Timothy 2025년 7월 2일
No, will give this a try, but out of curiousity why should cd be more fragile than addpath?
Timothy
Timothy 2025년 7월 2일
Ok, yes, that seems to work... thanks a bunch. I can't say I'm super happy with cd's unreliable functionality but glad for an alternate solution.

댓글을 달려면 로그인하십시오.

답변 (2개)

Matt J
Matt J 2025년 7월 2일
편집: Matt J 2025년 7월 2일
I can't say I'm super happy with cd's unreliable functionality but glad for an alternate solution.
It's not cd() that is at fault. cd() was never executed in any of your tests.
If the app cannot be found on the search path by Matlab, then the .mlapp code as a whole never gets executed, including any calls it makes to cd(). Double-clicking the .mlapp file in the FIle Explorer is a special case, only because, as you noted, it forces Matlab to launch with the app's parent folder as the Current Folder.
  댓글 수: 13
Steven Lord
Steven Lord 2025년 7월 3일
You forgot the name of the app. C:\Users\Matt\Downloads is a directory, not a file.
Sorry, rereading what I wrote I see now that "path to your mlapp file" could be ambigious, and you interpreted it as just the directory containing the file when I mean the path plus file name. That's my fault.
fullName = which('bench.m') % what I meant
fullName = '/MATLAB/toolbox/matlab/general/bench.m'
[pathToFile, filename, ext] = fileparts(fullName) % you used just pathToFile
pathToFile = '/MATLAB/toolbox/matlab/general'
filename = 'bench'
ext = '.m'
[filetype, opencmd, loadcmd] = finfo(fullName)
filetype = 'm'
opencmd = 'openm'
loadcmd = 0×0 empty char array
Matt J
Matt J 2025년 7월 3일
편집: Matt J 2025년 7월 3일
OK. I revised my last comment with proper execution of finfo.

댓글을 달려면 로그인하십시오.


Catalytic
Catalytic 2025년 7월 3일
The behavior makes sense to me, though I somewhat disagree with @Matt J. I think launching a .mlapp from the File Explorer is meant to be like running it as a stand-alone executable. Ideally, this would be done by opening a second Matlab session. However, I guess MathWorks doesn't want the user to incur the overhead of starting up and shutting down Matlab. So as a shortcut, they use any Matlab session that is currently already running. To make this work however, they do have to run some cleanup process to make sure that the effects of the startup function on the existing Matlab session are temporary.
  댓글 수: 1
Timothy
Timothy 2025년 7월 3일

Appreciate all the responses, unfortunately I am away from a computer for the next three days and cannot apply the test suggested by Steven Lord. On my computer, though, if I run an klapp as described and the mlapp is not in the path already the UI appears but I get class errors thrown if any button is pressed because it is not recognizing the class associated with the mlapp. This is on 2024a, with the specific prodedure I described above. Maybe the behavior has changed since then.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Package and Share Apps에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by