Reading multiple csv files and save in a matrix

Hello, I have multple csv files in a folder, I want to read them and store in a matrix, further, I also check for any duplicate entry in different files and sort the data in according to year and day. I am attaching one of the csv file here.
I tried foll. code:
SL_files = dir(sprintf('%s%s%s',fullfile(dirName),'\','*.csv'));
for idx = 1:size(SL_files,1)
disp(SL_files(idx,1).name)
fid = fopen(sprintf('%s%s%s',fullfile(dirName),'\',SL_files(idx,1).name));
filedata{idx} = textscan(fid, '%f%f%f %f:%f %f %f','Headerlines',16);
fclose(fid);
end
However, I am getting empty values after column 4. Any help? Thanks!

댓글 수: 1

I solve the problem:
numMat_All = [];
for idx = 1:size(SL_files,1)
disp(SL_files(idx,1).name)
fid = fopen(sprintf('%s%s%s',fullfile(dirName),'\',SL_files(idx,1).name));
data = textscan(fid, '%s %f %f %f %f %f %f', ...
'Delimiter',',', 'MultipleDelimsAsOne',1,'headerlines',16);
fclose(fid);
CharCell = data{1,1};
result = regexprep(CharCell,'[\s;:]+',' ');
numMat = cell2mat(cellfun(@str2num, result(:,1:end), 'UniformOutput', false));
numMat_All = [numMat_All;numMat];
data = []; CharCell = []; result = []; numMat = [];
end
dt = datetime([numMat_All(:,1:5), repmat(0,length(numMat_All),1)]);
T = table(dt,numMat_All(:,[6:7]));
T1 = sortrows(T,'dt');

댓글을 달려면 로그인하십시오.

답변 (1개)

KSSV
KSSV 2017년 10월 12일

0 개 추천

Use either xlsread or csvread to read the files.

댓글 수: 3

Poulomi Ganguli
Poulomi Ganguli 2017년 10월 12일
편집: KSSV 2017년 10월 12일
xlsread/csvread will not work in this case since the sheet has a combination of numeric and text values. I have attached sample sheet for reference and referred foll. link earlier:
KSSV
KSSV 2017년 10월 12일
On using xlsread you can extract the numeric data, text data separately.
It won't work since year and time are in different format. Textscan will work in this case. I am looking for some other answer/ way to solve it. Thanks!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

질문:

2017년 10월 12일

댓글:

2017년 10월 13일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by