Clear Filters
Clear Filters

Manipulating multiple csv files

1 view (last 30 days)
mabinj
mabinj on 30 Aug 2017
Edited: KSSV on 1 Sep 2017
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 Comment
mabinj
mabinj on 1 Sep 2017
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)

Sign in to comment.

Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!