Reading portions of several CSV files
7 views (last 30 days)
Show older comments
Aaron Carpenter
on 14 Aug 2019
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 Comments
Accepted Answer
Neuropragmatist
on 14 Aug 2019
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.
More Answers (2)
Rick Amos
on 15 Aug 2019
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 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!