Read multiple .CSV files with csvread using a for loop
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hello all, I'm have some trouble developing an algorithm to read multiple files at once and store them in different matrices. Heres a little, but sufficient, description of the problem:
Say I have n files named i_p.CSV, where n is a large enough number to do this procedure manually and i is a number that goes from 1 to n. The file i_p.CSV is a csv file with three columns, so what I would end up have on MATLAB is a matrix called data_i, which would be a (m x 3) matrix, where m is also a very large number.
Any tips on how to proceed?
채택된 답변
dpb
2018년 3월 24일
d=dir('*_p.clv'); % dir struct of all pertinent .csv files
n=length(d); % how many there were
data=cell(1,n); % preallocate a cell array to hold results
for i=1:n
data(i)=csvread(d(i).name); % read each file
end
Now, iff every one of the n .csv files has the same number of rows you can convert to an array...
data=cell2mat(data);
If they aren't all the same length you'll have to augment the shorter to the length of the longest or truncate the longer to that of the shortest.
댓글 수: 8
dpb, it worked mate! Thanks a lot! Sorry for taking this long to answer I've been pretty busy. In case someone else tried this, for some reason I recommend you guys to add path before! Something like
addpath('/...')
Hey there, I am having the same issue and I am not able to read all of the csv files.
The part of code that I dont get is:
data(i)=csvread(d(i).name);
what would "name" be in this case?
Thank you in advance..
name is the filename field in the directory information structure returned by dir().
Each iteration thru the loop will return the next filename found and returned in struct d
I am still unable to read all my csv files. So here's my problem:
I have around 9000 folders named ACCEDE00000 to ACCEDE08999 and each of them contains multiple csv files, but I'm only interested in one specific csv file named frame_table1_final.csv for each of 9000 ACCEDE videos. So I tried:
d=dir('/ACCEDE*/frame_table1_final.csv');
n=length(d);
data=cell(1,n);
for i=1:n
data(i)=csvread(d(i).name);
end
The error I get:
Error using csvread (line 35)
File not found.
Error in emotionn (line 5)
data(i)=csvread(d(i).name);
P.S. I am willing to import them as numeric arrays. Each of the csv files has same number of columns but different number of rows.
Thank you again!
Per the doc for dir() the filename is in .name; the folder/subdirectory name is in the .folder field.
If the file(s) are not in the working directory or on the matlabpath, you must fully-qualify them...
data(i)=csvread(fullfile(d(i).folder,d(i).name));
Hey all.. I completely understand reading Multiple .csv files. But i have a different issue. When I'm reading these files one by one, I need to skip certain lines in the begininning ( whicjh i managed to do so by using 'HeaderLines' command) and a last line, specifically the last row. Could someone help me sort this out please..
No truly practical way to skip the last line; simply delete it from the read array after reading the file.
This answer is incorrect. The for loop should rather read:
data{i} = csvread(d(i).name);
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
