Manipulating multiple csv files

4 ビュー (過去 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 Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by