Manipulating multiple csv files

조회 수: 2 (최근 30일)
mabinj
mabinj 2017년 8월 30일
편집: KSSV 2017년 9월 1일
I'm not sure if this has already been asked, but I couldn't find an answer. I have up to 600 csv files all with two columns. I want to input the second column of all of these data files into matlab. I then want to stick all these data sets into a table with separate columns for each data set/csv file and then finally plot them all against the same X values (first column in all of the csv files). I have seen an answer previously stating that this code would work for multiple inputs files, sticking them together essentially. I am new to all programming languages and am struggling with what seems like an easy task, is there a MATLAB Answers page I should be reading? Many thanks for any help.
'C:\DATA\Jess\CSV files\29-8-17 csv';%
if ~ isdir(files)%
errormessage = sprintf('Error: The following file does not exist:\n&s',files);
uiwait(warndlg(errormessage));
return;
end
filepattern = fullfile(files, '*.csv');
thefiles = dir(filepattern);
for j = 1: length(thefiles);
filename = thefiles(j).name;
fullfilename = fullfile(files, filename);
fprintf(1, 'Now reading %s\n', fullfilename);
dataArray = csvread(fullfilename);
if j == 1
allDataArray = dataArray;
else
allDataArray = [allDataArray; dataArray];
end
end
xlswrite(outputfilename, allDataArray, 'All Data', 'A1');
  댓글 수: 1
mabinj
mabinj 2017년 9월 1일
If interested I found a solution to this problem, the code is as follows:
% code
'C:\DATA\Jess\CSV files\29-8-17 csv';%
if ~ isdir(files)%
errormessage = sprintf('Error: The following file does not exist:\n&s',files);
uiwait(warndlg(errormessage));
return;
end
filepattern = fullfile(file,'*.csv');
thefiles = dir(filepattern);
for j = 1: length(thefiles);
filename = thefiles(j).name;
fullfilename = fullfile(files, filename);
fprintf(1, 'Now reading %s\n', fullfilename);
if j == 1
dataArray = csvread(fullfilename);
x = dataArray(:,1)
y = dataArray(:,2)
else
y = [y , csvread(fullfilename,0,1)];
end
end
plot(x,y)

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by