how to open a file from the previous folder
조회 수: 103 (최근 30일)
이전 댓글 표시
Hello, I have a folder hierarchy like the one in the image, I'm running a script from folder B and what I want is to open an image that is in folder C, but the path to it can change, that is, I want the code to be the same regardless of whether the folder A is on the desktop or in my documents for example.
Does anyone know how to program that?
댓글 수: 0
채택된 답변
Geoff Hayes
2020년 6월 12일
Alajendro - you should be able to use pwd to determine the current folder, and then "navigate" up one directory to A and then down one to C. I'm assuming that you know the name of folder C.
currentPath = pwd;
folderCName = 'someFolderName';
pathToFolderC = [pwd filesep '..' filesep folderCName filesep];
I had thought about using fullfile to build the pathToFolderC but (on my version of MATLAB),the '..' was not being included in the path.
추가 답변 (2개)
Amine BEROUAKEN
2024년 1월 31일
Hello, you can use this code
imagePath = fileparts(pwd) + "\FloderCName\imageName.png";
댓글 수: 0
Stephen23
2024년 1월 31일
Note that there is absolutely no need to call PWD. Simply use '.' to refer to the current directory:
relativePathToC = './../C';
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Search Path에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!