필터 지우기
필터 지우기

splitting data to two csv sheet

조회 수: 3 (최근 30일)
roozbeh yousefnejad
roozbeh yousefnejad 2018년 6월 11일
댓글: Paolo 2018년 6월 11일
Hi all, I have one csv file and I am planning to split it into two separate sheet based on a criteria. Th first column of my CSV has "name of the file" and all other columns have numbers. For each row, I want to check the last column and if it is more than 2300, separate the row and put it in one sheet of CSV, else put it in another sheet of CSV. I came up with this code
jj=1
cc=1
for j=1 : 463
if result(j,35)<2380
LF(jj,:)=result(j,:);
Lname{jj,1}=result1{j,1};
jj=jj+1;
else
HF(cc,:)=result(j,:);
Hname{cc,1}=result1{j,1};
cc=cc+1
end
end
sheet1=1;
sheet2=2;
xlswrite('seperated',Lname,sheet1,'A1')
xlswrite('seperated',LF,sheet1,'B1')
xlswrite('seperated',Hname,sheet2,'A1')
xlswrite('seperated',HF,sheet2,'B1')
It works, however, I am not sure when I want to write it in csv file, why I cannot not write the Lname which is a cell and include the related name can you please advise what I need to do to write it?
  댓글 수: 10
Guillaume
Guillaume 2018년 6월 11일
t = readtable('result.csv');
c = table2cell(t);
r = cell2mat(c(:,36))>2300;
That's a lot of unnecessary conversions here
t = readtable('result.csv');
r = t{:, 36} > 2300;
would achieve the same faster. There is no point in converting the table to anything else. You can just split the table itself.
Paolo
Paolo 2018년 6월 11일
That's a very good point.

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

채택된 답변

Guillaume
Guillaume 2018년 6월 11일
t = readtable('result - Copy.csv');
flag = t{:, 36} > 2300;
writetable(t(flag, :), 'datagreater.xls', 'WriteVariableNames', false);
writetable(t(~flag, :), 'datalower.xls', 'WriteVariableNames', false);
Should be all that is needed.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by