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

1 view (last 30 days)
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 Comment
Walter Roberson
Walter Roberson on 3 Sep 2013
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?

Sign in to comment.

Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!