필터 지우기
필터 지우기

split array to subarray and save it as csv file

조회 수: 2 (최근 30일)
Az.Sa
Az.Sa 2023년 3월 7일
댓글: Voss 2023년 3월 8일
Hi,
I have a csv file 6 x1086 I want to split it to 181 csv files each files 6x6 and save those files.
the first csv file will have the first 6 columns 1:6 , the second csv file will have the second 6 columns i.e. 7:12 ...etc.
I want to give name for each csv file for example: result1.csv , result2.csv ,...,result181.csv
Can you please help me with that. Thanks in advance

채택된 답변

Voss
Voss 2023년 3월 7일
fn = 'your\csv\file.csv';
pn = 'folder\where\you\want\the\results\to\go';
n = 6;
C = readcell(fn);
for ii = 1:size(C,2)/n
writecell(C(:,(ii-1)*n+(1:n)),fullfile(pn,sprintf('result%d.csv',ii)));
end
  댓글 수: 6
Az.Sa
Az.Sa 2023년 3월 8일
편집: Az.Sa 2023년 3월 8일
Hi,
if my matrix C is 6 x 4005
ans I want to split it to submatrix with following formula
frist submatrix c1: the first 6x230 observations
c1=C(:,1:230);
the second submatrix c2: is 6 x 230 observations as well but I will delet the first 20 observations from c1 and keep the rest 200 and add to them the following 20 observations from C. i.e.
c2=C(:,21:250);
the third submatrix c3: 6x230 observation, we remove the first 20 observation from c2 ,keep the 200 from c2 and add the following 20 observation from C . i.e.
c3=C(:,40:270);
and so on for the rest of the submatrix. so I am expecting to have 181 submatrix.
I tried to to change n=230 but in total I received only 17 submatrix which is only taking the 230 observations from C succesively which I beleive because of the formula C(:,(ii-1)*n+(1:n) )
Can you please help how to adjust the formula. Thank you
Voss
Voss 2023년 3월 8일
n = 230;
m = 20;
for ii = 1:(size(C,2)-n)/m+1
writecell(C(:,(ii-1)*m+(1:n)),fullfile(pn,sprintf('result%d.csv',ii)));
end
This will give you 189 sub-matrices: C(:,1:230), C(21:250), C(41:270), ..., C(3741:3970), C(3761:3990)
(3761-1)/20+1 = 189
And the last one ends with 3990 not 4005, i.e., the last 15 columns of C will be left over.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by