Reading portions of several CSV files
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi.
I am using a software that exports CSV files of strain data from selected points throughout a test. (for example: aug6_6_point1.csv, aug6_6_point2.csv,... etc). The first 6 rows of each file are useless header text, so what I want to end up with is a 3D array (timesteps X straindata X #ofpoints). Then I'd like to be able to use the data from each column to do calculations.
Right now I have matlab reading one csv just fine, I would just like to figure out how to do it in a loop to create one large array. (Using num2string or sprintf?)
data = csvread('aug6_6_point5.csv',6,0);
.
.
I dont mind entering the number of files every time, I just dont want to manually select the file each time.
Any help would be awesome.
Thanks in advance.
댓글 수: 0
채택된 답변
Neuropragmatist
2019년 8월 14일
Is the problem that the loaded data are not in a format you like and you can't concatenate it easily or is it that you just don't know how to implement a loop?
If the former is the problem uploading one of your .csv files would let us see the problem better.
Otherwise would something like this not work:
fnames = {'filename1.csv','filename2.csv','filenameX.csv'}; % cell array of the filenames you want
all_data = []; % you can preallocate this if you know what size to expect
for ff = 1:length(fnames)
data = csvread(fnames{ff},6,0);
all_data = [all_data; data];
end
Thanks,
M.
추가 답변 (2개)
Rick Amos
2019년 8월 15일
You might want to take a look at tabularTextDatastore and/or tall arrays. These are geared up to do exactly this kind of thing:-
fnames = {'filename1.csv','filename2.csv','filenameX.csv'};
% This is similar to csvread, but allows you to specify multiple files
ds = tabularTextDatastore(fnames, 'NumHeaderLines', 6, 'ReadVariableNames', false);
% If you just want to read all the data into a matrix
data = readall(ds);
data = data.Variables;
% Or, if you want to work with the data without reading all of it into memory in one go
data = tall(ds);
data = data.Variables;
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!