Search code examples
matlabioscanftextscan

Read line delimited by comma and tab


I would like to read files containing numbers in each line. Here is the example of the format-

0,0,0   1   0   0   0
0.02,0.1,0.98   8.77    0.985292    0.112348    0.112348
0.04,0.2,1.96   8.77    0.985292    0.112348    0.224696

As above shown, the first three numbers are separated by commas, after that all the rest numbers are separated by tab in the line. As a result, it is not possible to use dlmread or textscan. Is there any way to solve it? Thanks!


Solution

  • Yes you should add two parameters in your function:

    Delimiter %choose the delimiter

    and

    MultipleDelimsAsOne %Treat Repeated Delimiters as One

    Option 1:

    Small "trick" you can select more than one delimiter if you give a structure as input: {',',' '}.

    Result = textscan(fileID,'%f %f %f %f %f %f %f','Delimiter',{',',' '},'MultipleDelimsAsOne',1);
    

    Option 2: (that should work)

    This time I don't use MultipleDelimsAsOne but I precise that the delimiter can be a comma or a tab (with \t).

    Result = textscan(fileID,'%f %f %f %f %f %f %f','Delimiter',{',','\t'});