Azzera filtri
Azzera filtri

How to Split a Text File into Many Text Files ?

5 visualizzazioni (ultimi 30 giorni)
Priya
Priya il 15 Apr 2013
Risposto: Sheldon Ho il 12 Giu 2019
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 ?

Risposta accettata

Jan
Jan il 15 Apr 2013
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

Più risposte (1)

Sheldon Ho
Sheldon Ho il 12 Giu 2019
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});

Categorie

Scopri di più su Workspace Variables and MAT-Files in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by