How do I read strings with textscan?

14 次查看(过去 30 天)
kygreen
kygreen 2018-2-21
评论: kygreen 2018-2-21
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!

回答(2 个)

Walter Roberson
Walter Roberson 2018-2-21
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 2018-2-21
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 个评论
kygreen
kygreen 2018-2-21
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!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by