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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by