How do we automatically read current folder name in matlab script to read all data?

160 ビュー (過去 30 日間)
sam moor
sam moor 2017 年 3 月 17 日
回答済み: Dejia Kong 2019 年 7 月 8 日
I have attached below a part of my matlab script. The script is to read current sub-folders directory. As shown in the picture below "r20_" means the first 3 letter of current directory.
%get all subfolders of the main folders [stat,strf]=fileattrib; SubList = dir(fullfile(strf.Name, 'r20_*'));
Every time I change the current directory I need to edit the first 3 letter manually i.e. r20. I am thinking is there any way to read automatically the first three letter of current directory. Please note sublist in the part of code is used many times in rest of the code. So, while reading the three letter of current directory, sublist shouldn't affect for rest of the code. I appreciate your opinion. Thank you.

採用された回答

Guillaume
Guillaume 2017 年 3 月 17 日
編集済み: Guillaume 2017 年 3 月 17 日
pwd will give you the name of the current directory. It's then trivial to extract the 1st three letters:
currentdirectory = pwd;
currentdirectory(1:3)
  3 件のコメント
Guillaume
Guillaume 2017 年 3 月 17 日
currentdirectory = pwd;
SubList = dir(fullfile(strf.Name, [currentdirectory(1:3), '_*']));
Zhangxi Feng
Zhangxi Feng 2018 年 1 月 19 日
Umm.. pwd gives the full path. So pwd(1:3) will give the drive letter, such as F:\

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

その他の回答 (1 件)

Dejia Kong
Dejia Kong 2019 年 7 月 8 日
I think
[~,name,~]=fileparts(pwd);
gives the current name and you can deal with your problem with this.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by