How to open files in different folders for data processing?

68 ビュー (過去 30 日間)
Levi Wierman
Levi Wierman 2019 年 2 月 5 日
コメント済み: Stephen23 2019 年 2 月 9 日
Hello! I'm currently tasked with plotting data from several different files that are in different folders due to the way the data was processed. How can I script matlab to look through different folders and get a file (the file in each folder has the same name but the folders have different names) and grab the data that I am interested in?
I am very new to matlab so this is hard for me to try to do.
Thank you,
-Levi

回答 (2 件)

Bob Thompson
Bob Thompson 2019 年 2 月 5 日
編集済み: Bob Thompson 2019 年 2 月 5 日
Take a look at this.
It is possible to look through directories recursively with newer versions of matlab using the dir command, but the above should work for all relatively common versions in use.
  8 件のコメント
Levi Wierman
Levi Wierman 2019 年 2 月 8 日
編集済み: Jan 2019 年 2 月 9 日
I will say it again, I do not know how to make it work. You gave me a solution that looks like it requires me to give matlab the path directly to the file but that is not what I need to do... because it's in a bunch of folders. I am brand new to matlab and I have a hard time understanding minimalist syntax exmples in the operators. Is filename an array that has each file location or whatever in it? What is 1:nume1?
Jan
Jan 2019 年 2 月 9 日
編集済み: Jan 2019 年 2 月 9 日
@Levi: If you ask for details about my answer, please post them as comments to my answer.
The \*'*\ is the search string of the dir command means a recursive search in all subfolders.
Please take the time to look into th documentation:
help numel
The last character is an lowercase L, not a one.
You cannot learn the basics in the forum. The Getting Started chapters of the documentation and the "Onramp" (ask an internet search engine) are more efficient.
You can simply run the code and check, what the contents of the variable FileName is.

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


Jan
Jan 2019 年 2 月 6 日
Since Matlab >= R2016b:
List = dir('C:\Your\Data\Folder\**\FileName.txt');
FileName = fullfile({List.folder}, {List.name});
for k = 1:numel(FileName)
disp(FileName{k})
...
end
  2 件のコメント
Levi Wierman
Levi Wierman 2019 年 2 月 9 日
I wasn't aware of the ** being the command to search recursively, I thought it was a shortcut to basically say "however long the path is." I'll try to make the dir command work now that I do not have a deadline looming!
Thank you.
Stephen23
Stephen23 2019 年 2 月 9 日
"I wasn't aware of the ** being the command to search recursively, I thought it was a shortcut to basically say ..."
tip for the future: guessing how MATLAB works is very inefficient. Reading the documentation is much better (and if something is not clear, as Jan already wrote, you can ask us to clarify).

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

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by