필터 지우기
필터 지우기

How to Split a Text File into Many Text Files ?

조회 수: 5 (최근 30일)
Priya
Priya 2013년 4월 15일
답변: Sheldon Ho 2019년 6월 12일
I have a text file which contains many Paragraphs.
Each Paragraph starts with a "greater than" ( > ) symbol.
I would like to split each paragraph into new text file using MATLAB.
Is it possible ?

채택된 답변

Jan
Jan 2013년 4월 15일
Does the ">" appear as first character in a line or anywhere? Assuming the first:
Str = fileread(FileName);
CStr = regexp(Str, '\n', 'split');
Index = [find(strncmp(CStr, '>', 1)), length(CStr) + 1];
for iP = 1:length(Index - 1);
FID = fopen(sprintf('Paragraph%2d.txt', iP), 'w');
if FID == - 1, error('Cannot open file for writing'); end
fprintf('%s\n', CStr{Index(iP):Index(iP + 1) - 1});
fclose(FID);
end
  댓글 수: 1
deeksha h r
deeksha h r 2016년 8월 14일
What if i want to split lines of a paragraph in an image..??

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

추가 답변 (1개)

Sheldon Ho
Sheldon Ho 2019년 6월 12일
The line: fprintf('%s\n', CStr{Index(iP):Index(iP + 1) - 1});
should be: fprintf(FID,'%s\n', CStr{1,Index(iP):Index(iP + 1) - 1});

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by