Modification of Current Folder in matlab

Reference:
https://www.cnblogs.com/shine-lee/p/4258863.html
Because when executing the matlab program, if the relative path is specified in the .m file, the relative path will not be found at runtime. the indicated data. To explain, our .m program is under the subdirectory Image Segmentation under Current Folder, and the .m program references the data under Image Segmentation\data. The directory structure is like this:
|——–Image Segmentation
|————-.m program
|————data
|————data
The specific example is: the full path name of the .m file, D:\matlab_workpace \matlab exercise book\Computer-Vision-Lung-Cancer-Detection-master\Image Segmentation.m program. But because the Current Folder is not under the Image Segmentation, the program cannot find the data folder. So report it as an invalid folder.
Although we have clicked on the .m program and are in an editable transition state, this does not mean that our Current Folder is the direct parent directory of the .m program. (Forgive my vagueness!!)
write picture description here
The following error occurs:
write picture description here

solve

method 1

Of course, you can use the simplest method and add the following sentence directly at the beginning of the .m program. Change the Current Folder to Image Segmentation so that it can be found when searching for data\.

cd 'D:\matlab_workpace\matlab练习册\Computer-Vision-Lung-Cancer-Detection-master\Image Segmentation';

Method 2 - Functions for Matlab Directory Operations

The functions of directory operation commonly used in matlab are as follows:
write picture description here
Of course, you can also write an adaptive code to change the Current Folder.

% 生成当前文件所在的完整目录,包括文件名
filename = mfilename('fullpath');
% 将文件完整路径进行分割 ,pathstr是目录,name是文件名
[pathstr,name,ext]= fileparts(filename);
cd(pathstr);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325877828&siteId=291194637