Effacer les filtres
Effacer les filtres

Manipulating multiple csv files

2 vues (au cours des 30 derniers jours)
mabinj
mabinj le 30 Août 2017
Modifié(e) : KSSV le 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 commentaire
mabinj
mabinj le 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)

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by