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
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 Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!