Import certain column from files in directory and save in multiple columns

1 回表示 (過去 30 日間)
Kundan
Kundan 2013 年 9 月 3 日
I am new to Matlab. I am trying to import multiple files from directory and import a specific column. The final output should be multiple columns. For e.g. I have file1.csv, file2.csv, file3.csv, etc. I need to read column 1 of each files and save it in final.csv which will have first column of each file
column 1 = 1st column of file1
column 2 = 1st column of file2
column 3 = 1st column of file3
so on and so forth..
Here's my code so far..I am struggling. Any help or direction is greatly appreciated. Thanks!
%%Read the files in the directory
dirData = dir('*.csv');
datFiles = {dirData.name};
nFiles = numel(datFiles);
fileName = char(datFiles);
for k = 1:nFiles;
delimiterIn = ' ';
headerlinesIn = 1;
newData1 = importdata(datFiles{k}, delimiterIn, headerlinesIn);
needed(:,k) = newData1.data(:,1);
end;
The error that I get is:
Attempt to reference field of non-structure array.
Edit: Changed curly bracket to small bracket in the second last line of the code and it works.
  1 件のコメント
Walter Roberson
Walter Roberson 2013 年 9 月 3 日
Which line does the problem occur on? Does the error happen on every file, or only one one particular file? What is class(newData1) when the problem occurs?

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by