MATLAB Answers


How to Split a Text File into Many Text Files ?

Asked by Priya
on 15 Apr 2013
Latest activity Commented on by deeksha h r
on 14 Aug 2016

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 ?



No products are associated with this question.

1 Answer

Answer by Jan Simon
on 15 Apr 2013
 Accepted answer

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});

  1 Comment

What if i want to split lines of a paragraph in an image..??

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!