Textscan File and ignoring the newline characters

How can I textscan a file and ignore newline characters?

4 commentaires

dpb
dpb le 17 Jan 2014
What do you mean by "ignore"?
textscan will apply the fmt string repetitively on the records in the file returning the data by itself with no newlines embedded.
Need short example to clarify the problem you're trying to solve.
Thank you! My example is the following:
"My","name","is","Giorgos","I","was","born","in","May
1985","I like matlab."
"My","name","is","Giorgos","I","was","born","in","May
1985","I like matlab."
"My","name","is","","I","was","born","in","May
1985","I like matlab."
"My","name","is","Giorgos","I","was","born","in","May
1985","",
I want to retrieve words contained within the quotation marks in batches of tens. So create a cell Nx10.
The problem is that the 9th phrase is going in the next line because there are the characters \r\n, so the textscan creates two cells instead of one.
'"May' '1985"'
instead of:
'"May 1985"'
Is it more clear?
What textscan command are you using?
tmp = textscan(fid, '%s%s%s%s%s%s%s%s%s%s' , 'delimiter', ',', 'headerlines', 1);

Connectez-vous pour commenter.

 Réponse acceptée

AJ von Alt
AJ von Alt le 17 Jan 2014
Modifié(e) : AJ von Alt le 17 Jan 2014
You should use %q instead of %s when double quotation marks are used to indicate text that you want to keep together. Additionally, the parameter 'HeaderLines' should be set to 0 when there are not header lines to skip.
The following code produced the desired result for the attached input.
% Open the file
fid = fopen ('testinput2.txt' );
% number of consecutive strings to read
nString = 10;
% create the datafield template
formatSpec = repmat( '%q' , 1 , nString );
% parse the file
parsedText = textscan( fid, formatSpec , 'delimiter',...
',' , 'headerlines', 0, 'CollectOutput', 0 );
% Clean up
fclose(fid);

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by