I have multiple CSV files and I am trying to make a new CSV file by combining the 4th column of each of the CSV file... but cant figure out how..csvwrite command is a bit confusing for me...Can someone please tell me how can I do that?

1 次查看(过去 30 天)
So i need to make a new csv file using a multiple csv files (only the 4th column of each csv file specifically and the first column is common in all....which i also want to be the 1st column of new csv file) but i am just stuck with constant errors i tried with this code
files= dir('*.csv');
out = csvread(files(1).'pmu6n7angoffset1.csv');
for ii = 2:numel(files)
new = csvread(files{ii}.'pmu6n7angoffset10.csv');
out = horzcat(out, new(:,2));
end
but it doesnt work
  1 个评论
Stephen23
Stephen23 2018-8-31
You really need to start reading the MATLAB documentation, because guessing how to write code is just going to be huge waste of your time.
  • when you read the dir help, it clearly lists all of the fields of the output structure: name, folder, bytes, etc. It does not list fields like pmu6n7angoffset1.csv or anything similar.
  • That syntax would not work anyway, because you wrote a character vector where there should be a fieldname (e.g. name, folder, bytes, etc). The MATLAB documentation explains how to access the fields of structures.
  • What is the point of hard-coding the name anyway? The whole point of dir is that it automatically collects all the filenames for you, so that you don't need to write filenames everywhere!
  • Why are you using cell array indexing to try and access a structure?: files{ii}....
  • Your text states that you want "...the 4th column of each csv file...", so why are you getting the second column?: new(:,2));

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2018-8-31
编辑:Stephen23 2018-8-31
Instead of guessing how to write MATLAB code it is much easier to follow the examples in the documentation:
Try something like this:
S = dir('*.csv');
C = cell(1,numel(S));
for k = 1:numel(S)
tmp = csvread(S(k).name);
C{k} = tmp(:,2); % 2 or 4 ?
end
M = horzcat(tmp(:,1),C{:}}

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by