How do I read strings with textscan?

41 visualizzazioni (ultimi 30 giorni)
kygreen
kygreen il 21 Feb 2018
Commentato: kygreen il 21 Feb 2018
I have the following format I'm trying to read from a file with textscan:
10000 'name1 name2 name3 ..' 3 4 5 6 7 8 9 10 11
20000 'name4 name5 ..' 3 4 5 6 7 8 9 10 11
30000 'name6 name7 name8 name9 ..' 3 4 5 6 7 8 9 10 11
repeat format for 150 lines
The issue is I want everything between single quotes to be read/stored as one string. The problem is that there are whitespaces and also sometimes there are 3 names, sometimes there are 2, sometimes 4.
I've tried:
formatSpec = '%d %s %f %f %f %f %f %f %f %f %f'
C = textscan(fid,formatSpec,150)
I know my formatSpec is too basic for strings with multiple whitespaces and words. Can you help with how I should read in this format? I basically want C to be an 150x11 cell matrix where column 2 is the entire string from single quote to single quote.
Thanks!

Risposte (2)

Walter Roberson
Walter Roberson il 21 Feb 2018
The trick to this is to use a format item
''%[^'']''
for each place you want one of those quoted strings.
But there is a different approach: use fileread to read the entire file into a string, and then replace all ' in the string with " and then textscan the string with a %q format element. (That is, you can pass a string as the first parameter to textscan instead of a file identifier)

C.J. Harris
C.J. Harris il 21 Feb 2018
Have you tried specifying the delimiter?
formatSpec = '%d %s %f %f %f %f %f %f %f %f %f';
C = textscan(fid,formatSpec,150, 'Delimiter', '''');
  1 Commento
kygreen
kygreen il 21 Feb 2018
Thanks! This was very helpful. I didn't get the delimiter syntax correct, and I also had the wrong N value so my results were awry. It is good now!

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by