Have data in subfolders available

39 ビュー (過去 30 日間)
Neko Benítez
Neko Benítez 2019 年 11 月 18 日
コメント済み: Neko Benítez 2019 年 11 月 20 日
I have a folder with a MATLAB script. In that folder, I have subfolders with information that can be processed with the MATLAB script. If I want to read a specific file, the only idea I have now is to specify the complete path for each file, with it is tedious.
What I would like is to have all those files available, in the same way that you have available all the data when you are working in the current folder. Is there any way to have available all the data in the subfolders (CSV files, TXT files, etc.)?

採用された回答

Guillaume
Guillaume 2019 年 11 月 18 日
編集済み: Guillaume 2019 年 11 月 18 日
Firstly, it's not a good idea to mix code and data folder. The two should be completely separate, so that your code can work regardless of the location of the data (on the local drive, on a usb drive, on a remote server, in the cloud...)
Secondly, it's not clear what you mean by "available". Matlab can access data in any folder and can easily list the content of any folder with dir. e.g. if you wanted to know all the .csv files in a specific folder you'd use:
folder = 'C:\somewhere\somefolder'
filelist = dir(fullfile(folder, '*.csv'));
%process the files one by one:
for fidx = 1:numel(filelist)
data = readtable(fullfile(filelist(fidx).folder, filelist(fidx).name)); %read the file
%... code to use the data
end
  7 件のコメント
Stephen23
Stephen23 2019 年 11 月 20 日
編集済み: Stephen23 2019 年 11 月 20 日
"..then select 'Add to Path'. If you do so, all the files in that folder are now available when you use the script."
The MATLAB Search Path should refer only to folders that contain MATLAB code. It is not intended for accessing data files: data files are most efficiently read using absolute/relative filenames, exactly as Guillaume showed.
Neko Benítez
Neko Benítez 2019 年 11 月 20 日
I am going to start working that way for now on. But until now, I always kept the data and script in the same folder/subfolder.
Thank you very much again!

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

その他の回答 (0 件)

カテゴリ

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