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 中查找有关 Text Data Preparation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by