I have 37 csv files. I need to read data from first row of all the 37 files and merge these 37 rows into one mat file.
Then I will read all the 2nd rows and merge them into one mat file.
I will keep reading the data till the last row of each csv file.
Name of my csv files are:
Col01_all
Col02_all
.
.
.
Col37_all
I am not sure how to do this.

댓글 수: 7

Adam Danz
Adam Danz 2019년 4월 23일
I suggest you read the entire files into matlab (in a loop) and then collate the rows as needed after the data are in your workspace.
Stephen23
Stephen23 2019년 4월 23일
편집: Stephen23 2019년 4월 23일
Read the files in a loop, using sprintf to generate the filenames:
As Adam Danz wrote, it would be easiest if you simply import the whole files and the use indexing to select the rows that you need.
"Read the files in a loop, using sprintf to generate the filenames:"
For that type of a set of file names, I'd use the dir solution with a wildcard as being simpler...
d=dir(fullfile(wkdir,'Col*all.csv')); % presuming there is a .csv extension
for i=1:numel(d)
import/process file fullfile(wkdir,d(i).name)) here
end
I have extracted the data from all csv files using the following code.
csvfiles = dir('*.csv') ;
N = length(csvfiles) ;
A = cell(N,1) ;
for i = 1:N
A{i} = csvread(csvfiles(i).name);
end
Now I have a cell array that contains all the data. A{1,1}, A{2,1},...A{37,1}.
I want to extract the first row from A{1,1}, A{2,1},...{37,1} and then combine all the 37 rows in one mat file. How can I do that?
KSSV
KSSV 2019년 4월 24일
Are you sure that all A{i} are of same size?
Ahmad Hasnain
Ahmad Hasnain 2019년 4월 24일
Yes, they are all of the same size 15x38

댓글을 달려면 로그인하십시오.

 채택된 답변

Guillaume
Guillaume 2019년 4월 24일

0 개 추천

%your original code, slightly modified
csvfiles = dir('*.csv');
filescontent = cell(numel(csvfiles), 1);
for i = 1:numel(csvfiles)
filescontent{i} = csvread(csvfiles(i).name);
end
%write rows of matrices into mat file
destfolder = 'C:\somewhere\somefolder';
allcontent = cat(3, filescontent{:}); %concatenate all matrices into a 3D matrix
%save rows of each matrix into a mat file (as variable rows)
for rowidx = 1:size(allcontent, 1)
rows = permute(allcontent(rowidx, :, :), [3 2 1]); %extract rows.Permute so each matrix is a row of rows
save(fullfile(destfolder, sprintf('row%d.mat', rowidx)), rows);
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

태그

질문:

2019년 4월 23일

답변:

2019년 4월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by