How do I create a new line after every 3 characters in a text file?
조회 수: 4 (최근 30일)
이전 댓글 표시
If I had a .txt file that has one line as
abcdefghijklmnopqrstuvwxyz
I would like either to change or create a new .txt file that would look like this:
abc
def
ghi
jkl
mno
pqr
stu
vwx
yz
So, it's essentially reading 3 characters from the right, creating a new line '\n', going to the second line, reading 3 characters, creating a new line, etc.
Thank you!
댓글 수: 0
답변 (1개)
ES
2014년 1월 23일
fileID=fopen('InputFile.txt','r');
AllText = fscanf(fileID, '%s')
TextLength=length(AllText);
NoOfLines=ceil(TextLength/3);
OutFileID=fopen('OutputFile.txt','w');
for l1=0:NoOfLines
try
SrcString=AllText((l1*3)+1:(l1*3)+3)
catch
SrcString=AllText((l1*3)+1:end)
end
fprintf(OutFileID,'%s\r\n',SrcString);
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!