Question

I am reading a tab-delimited file. Five representative lines of this file are:

Date Time Property Path 1 Path 2 Path 3 Path 4 Path 5 Path 6 Path 7 Path 8
Lev 1 Lev 1 Lev 1 Lev 1 Lev 1 Lev 1 Lev 1 Lev 1

1/1 00:00:00 F1 (sm³/s) -1.3405E-003 -1.1170E-002 -1.0123E-004 9.7769E-003 -8.4673E-004 1.1710E-003 2.6890E-004 2.2413E-003

1/1 01:00:00 F1 (sm³/s) 1.9988E-004 1.6655E-003 2.2252E-004 1.6883E-003 1.8612E-003 2.0221E-004 2.0795E-004 1.7333E-003

1/1 02:00:00 F1 (sm³/s) -4.0722E-004 -3.3931E-003 -4.4324E-004 -2.1177E-003 -3.7075E-003 -2.5364E-004 -3.7330E-004 -3.1115E-003

When I use the following format string I get the expected results:

test = '1/1 00:00:00    F1 (sm³/s)  -1.3405E-003    -1.1170E-002    -1.0123E-004    9.7769E-003 -8.4673E-004    1.1710E-003 2.6890E-004 2.2413E-003';

textscan(test, '%*s %*s %*s %*s %f %f %f %f %f %f %f %f')

Gives me:

ans = 

[-0.0013]    [-0.0112]    [-1.0123e-04]    [0.0098]    [-8.4673e-04]    [0.0012]    [2.6890e-04]    [0.0022]

Which is what I want, but when I attempt:

    C = textscan(fid,...
             '%*s %*s %*s %*s %f %f %f %f %f %f %f %f',...
             'CollectOutput', false,...
             'Headerlines', 2);

I get a 1x8 cell of empty cells.

What is the error in the format string translation?

Was it helpful?

Solution

I don't think there's anything wrong with your format string specifically.

Try pulling in the lines individually with fgetl or similar and just check that there's nothing you weren't expecting in the file. For example - your code seems to work for me but I can replicate your error by putting an additional blank line at the start of the file, which causes textscan to try and read the second header line as a line of data (and fail inelegantly). That particular error can be removed by increasing the value of HeaderLines.

fid = fopen('test.txt');
fgetl(fid)  % repeat until you see your first line of data

OTHER TIPS

Now, I try to use you code and it's work!

file=('d.txt');
fid=fopen(file);
C = textscan(fid,...
         '%*s %*s %*s %*s %f %f %f %f %f %f %f %f',...
         'CollectOutput', false,...
         'Headerlines', 2);

Output:

celldisp(C)

C{1} =

            -0.0013405
            0.00019988
           -0.00040722

C{2} =

              -0.01117
             0.0016655
            -0.0033931

C{3} =

           -0.00010123
            0.00022252
           -0.00044324

C{4} =

             0.0097769
             0.0016883
            -0.0021177

C{5} =

           -0.00084673
             0.0018612
            -0.0037075

C{6} =

              0.001171
            0.00020221
           -0.00025364

C{7} =

             0.0002689
            0.00020795
            -0.0003733

C{8} =

             0.0022413
             0.0017333
            -0.0031115

I came across a problem where my textscan would only grab empty cell arrays, google search led me here. I solved it by using fgetl(fid) a couple of times and then frewind(fid), (fid being your variable for fopen) something about reading the lines made it easier to bring in the values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top