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?

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by