필터 지우기
필터 지우기

I have a txt file containing a Matrix separated by commas. I would like to add a comma after the last column on every row.

조회 수: 4 (최근 30일)
I have a .txt file containing >200'000 rows and 12 columns separated by commas (only numbers, no characters). I would like to add a comma after the last column on every row without adding any values in a 13. column. (example below).
example of what I have:
1,2,3,4,5,6,7,8,9,10,11,12
1,2,3,4,5,6,7,8,9,10,11,12
example of what I am trying to get:
1,2,3,4,5,6,7,8,9,10,11,12,
1,2,3,4,5,6,7,8,9,10,11,12,
Thank you already for your time.

채택된 답변

dpb
dpb 2022년 4월 28일
편집: dpb 2022년 4월 30일
data=readlines('yourfile.txt'); % read file as string array
data=data(strlength(data)>0); % save only non-empty lines
data=strcat(data,","); % catenate "," on end
writematrix(data,'yournewfile.txt','QuoteStrings',0) % write back out, don't quote
  댓글 수: 4
dpb
dpb 2022년 4월 28일
편집: dpb 2022년 4월 30일
I did just check -- it is, indeed, whether or not there is that last newline or not.
The above fix will remove it if is there in the original; leaving it out will produce the extra ','; a little extra logic could have both by testing if there is a blank line and only appending the ',' to those lines that are longer.
data=readlines('yourfile.txt'); % read file as string array
ix=strlength(data)>0; % find non-empty lines
data(ix)=strcat(data(ix),","); % catenate "," on end
writematrix(data,'yournewfile.txt','QuoteStrings',0) % write back out, don't quote

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by