1

I have a file which contains data in the following format 0,"20 300 40 12".

How can I read this data with sscanf function such that I store 0 in a separate variable and 20 300 40 12 in another variable. The problem is that the array within the " " changes its size, so I cannot use a fix length array. So I can have something like this within my file:

0,"20 300 40 12"
0,"20 300 43 40 12"
1,"22 40 12"

Can you give me a hint of how to read this?

Amro
  • 123,847
  • 25
  • 243
  • 454
Simon
  • 4,999
  • 21
  • 69
  • 97
  • similar question: http://stackoverflow.com/questions/16242074/textscan-in-matlab-when-delimiter-is-in-a-field-and-what-to-ignore-character – Amro Apr 27 '13 at 18:35

2 Answers2

2

Have you tried with this:

fid = fopen(filename,'r');  
A = textscan(fid,'%d,%q','Delimiter','\n');
fpe
  • 2,700
  • 2
  • 23
  • 47
0

Here's another way to do it:

[a,b] = textread('ah.txt','%d,"%[^"]"');
fun = @(x) split(' ',x);
resb = cellfun(fun,b,'UniformOutput',false)
res = {a resb};

function l = split(d,s)
%split string s on string d
out = textscan(s,'%s','delimiter',d,'multipleDelimsAsOne',1);
l = out{1};
carlosdc
  • 12,022
  • 4
  • 45
  • 62