Reading portions of several CSV files
5 次查看(过去 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!