Write sections of a long string as new lines in a text document

조회 수: 3 (최근 30일)
David Haydock
David Haydock 2022년 1월 18일
답변: Steven Lord 2022년 1월 18일
I have a very long string value, approx. 13,000 characters long, that is made up of the letters X, Y and Z. So:
sub = 'XYZYXYZYZYZXYZYXYZ....'
and so on. Within the long string, there is also every so often a 0:
sub = '...X0Y...'
The 0 represents a break point. I need to write out this long sequence onto a single txt file, but at each instance where there is a '0', I need to remove that zero and start a new line on the text file.
How would I go about doing this?

채택된 답변

Stephen23
Stephen23 2022년 1월 18일
sub = 'XYZYXYZYZYZXYZYXYZ0XYXYXYX';
tmp = split(sub,'0');
fprintf('%s\n',tmp{:})
XYZYXYZYZYZXYZYXYZ XYXYXYX
  댓글 수: 1
Stephen23
Stephen23 2022년 1월 18일
Where of course you replace that FPRINTF line with:
fid = fopen('myfile.txt','wt');
fprintf(fid,'%s\n',tmp{:});
fclose(fid);

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

추가 답변 (1개)

Steven Lord
Steven Lord 2022년 1월 18일
sub = 'XYZYXYZYZYZXYZYXYZ0XYXYXYX';
sub2 = replace(sub, '0', newline)
sub2 =
'XYZYXYZYZYZXYZYXYZ XYXYXYX'

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by