matlab How to set the path where the .m file is located to the current active directory (current folder) and save it to the matlab search path

When writing a matlab program, in order to facilitate porting, you can set the directory where the test.m file is located as the active window. You can also set the directory where the test.m file is located as one of the matlab search paths based on this idea. The following explains how to programmatically set up in test.m to achieve the purpose of setting the directory where the test.m file is located as the active window.

It's actually very simple: you just need to add the following code to the test.m file that needs to be run first:

% %Set the path of this file to the current workspace path
filep = mfilename('fullpath'); %filep contains the path of this m file and the file name (without .m suffix)
[pathstr,namestr]=fileparts( filep);%pathstr is the path where this m file is located
cd(pathstr);%Change the current active directory path

Because the path of test.m is already known, you can also set the path of this file to the path searched by matlab:

addpath(pathstr);%Add the path and you can do it

res =savepath; %Then save, you can directly run other .m files in this path next time

Guess you like

Origin blog.csdn.net/liuxhCSDN/article/details/88696401