To change Current Folder to the folder of .m File
211 次查看(过去 30 天)
显示 更早的评论
How can I change the current folder to folder of .m file in my code.
Possibly with cd command
Or any command I can indicate that my excel file is in same location as .m file.
e.g. code.m path folder\My excel file.xls
0 个评论
回答(4 个)
Image Analyst
2013-4-17
编辑:Image Analyst
2013-4-17
Here's some code I got from Brett Shoelson of the Mathworks:
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
Edit: Note that the isdeployed is important if you will be compiling and deploying your application. This let's it operate both for you, as the developer, and for your deployed application where it will skip the cd because doing cd like that in a deployed app can wreak havoc. If you won't be doing compiling, then you can do just the cd without the if.
3 个评论
Yannis Axaopoulos
2022-11-26
编辑:Yannis Axaopoulos
2022-11-27
This command that ran fine in my MATLAB R2015b, is not actually working in the newest R2022a!
EDIT:
Actually find the solution in an other answer
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(matlab.desktop.editor.getActiveFilename));
end
Diaa
2021-5-15
1 个评论
Javier Ros
2022-3-4
This works, but the problem is that most people expects code in .m file to work in .mlx and viceversa.
I think that this issue should be adressed by MATLAB's guys.
Best regards,
Javier
Florian
2018-7-13
mfile_name = mfilename('fullpath');
[pathstr,name,ext] = fileparts(mfile_name);
cd(pathstr);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!