how to open a file from the previous folder

101 ビュー (過去 30 日間)
Alejandro Fernández
Alejandro Fernández 2020 年 6 月 12 日
コメント済み: Stephen23 2024 年 1 月 31 日
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?

採用された回答

Geoff Hayes
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.
  7 件のコメント
Geoff Hayes
Geoff Hayes 2020 年 6 月 13 日
No problem! :)
Stephen23
Stephen23 2024 年 1 月 31 日
" had thought about using fullfile to build the pathToFolderC but (on my version of MATLAB),the '..' was not being included in the path."
Odd. It works here:
folderCName = 'someFolderName';
pathToFolderC = fullfile(pwd,'..',folderCName)
pathToFolderC = '/users/mss.system.g8rKA4/../someFolderName'

サインインしてコメントする。

その他の回答 (2 件)

Amine BEROUAKEN
Amine BEROUAKEN 2024 年 1 月 31 日
Hello, you can use this code
imagePath = fileparts(pwd) + "\FloderCName\imageName.png";

Stephen23
Stephen23 2024 年 1 月 31 日
Note that there is absolutely no need to call PWD. Simply use '.' to refer to the current directory:
relativePathToC = './../C';

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by