My CSV files are spectra data of the type A1a(1:853,1:853)
. Column 1 contains wavelength information, Column 2 contains intensity information.
I have multiple CSV files in a folder, A1a, A1b, A1c.....A1k.csv
and I would like to store these files in an array of type A(10,1:853,1:853)
using csvread
and a for
loop.
How can I do this?
You can use dir
with dlmread
(or csvread
, a wrapper for dlmread
) to read in your data. This example assumes the size of each data file is uniform and you know the exact structure of your data:
% Generate some sample data
fID = fopen('A1a.csv', 'w');
fprintf(fID, '%u,%u,%u\n', repmat(1:5, 3, 1));
fclose(fID);
fID = fopen('A1b.csv', 'w');
fprintf(fID, '%u,%u,%u\n', repmat(6:10, 3, 1));
fclose(fID);
fID = fopen('A1c.csv', 'w');
fprintf(fID, '%u,%u,%u\n', repmat(11:15, 3, 1));
fclose(fID);
% Preallocate array if you know for sure how your data is structured
nfiles = 3;
nrows = 5;
ncols = 3;
mydata = zeros(nrows, ncols, nfiles);
% Read in your data
nheaderlines = 0;
nskipcolumns = 0;
filestoread = dir('A1*.csv');
for ii = 1:length(filestoread)
mydata(:, :, ii) = dlmread(filestoread(ii).name, ',', nheaderlines, nskipcolumns);
end
This gives us mydata
as a [5x3x3]
array, where mydata(:,:,1)
is A1a.csv
, mydata(:,:,2)
is A1b.csv
, and mydata(:,:,3)
is A1c.csv
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.