How to open files in different folders for data processing?
이전 댓글 표시
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
2019년 2월 5일
편집: Bob Thompson
2019년 2월 5일
1 개 추천
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
2019년 2월 6일
Bob Thompson
2019년 2월 6일
The link presented is for a matlab code. Basically, you feed it a parent directory (the highest level that contains subdirectories with the files you want) and a string check (something that relates to your files). It will then find all files with that string in the parent directory, and any subdirectories within the parent directory (no matter how many levels deep). The results will be a list of file names, similar to what you get from 'dir'.
files = findfiles('myfile.csv','C:/Users/Me/Work');
From there you can just run a loop through the list of files and conduct whatever processing you need on each one.
Levi Wierman
2019년 2월 7일
You cannot leave an input argument empty in Matlab:
datamatrix=findfiles('discharge_data.dat', ...
'C:\Users\LeviW\Documents\PlasmaData',);
% ^^
This is not allowed. You can omit a trailing input argument, but then the comma is left out also. See the examples included in the function.
Matlab is case-sensitive and as the error message tells clearly, the function is called "FindFiles" with uppercase Fs.
Why don't you use the dir function, which works recursively since R2016b?
Levi Wierman
2019년 2월 8일
Levi Wierman
2019년 2월 8일
편집: 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
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
2019년 2월 9일
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).
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!