How to make the textscan command skip the line with texts

Discussion in 'Programmer's Corner' started by talhak, Jun 13, 2011.

  1. talhak

    Thread Starter New Member

    May 30, 2011
    4
    0
    hi,
    anybody know how the textscan can be used to skip the text line in file and carry on reading the data in a sequence. I have to read a file with numeric data but the text appears randomly in the file. i have to skip and read the next line.

    But one thing i have observed that once the textscan reads the text file i keeps on returning empty array.

    here the code:

    fname = input('Enter Data File Name:','s');
    fid=fopen(fname,'r');
    r=zeros(300,8);

    for j=1:300

    C = textscan(fid, '%d', 8);
    m = cell2mat(C);

    r1=m';
    r(j,:)=r1;
    j=j+1;
    if j==300
    break;
    end;

    end
    plot(r(2:300,1));

    So, the code reads the data up to 300 samples, but the text appears 2 or 3 times for two rows. Once the textscan reads the text it gives error and returns empty array. The data is like that:
    -12 -42 40 -75 -70 -110 -153 -140
    -4 -55 -18 -144 -130 -183 -234 -207
    17 -22 -41 -161 -131 -190 -221 -179
    -62 -92 16 -89 -71 -96 -129 -109
    -38 -64 89 -17 -31 -35 -95 -73
    -80 -71 43 -68 -65 -94 -147 -134
    -45 -59 -11 -124 -122 -189 -216 -180
    -62 -69 -42 -157 -128 -195 -228 -186
    -65 -81 53 -42 -32 -45 -88 -49
    -79 -84 117 19 19 -3 -53 -35
    -39 -52 44 -54 -46 -76 -121 -105
    -22 -32 1 -119 -118 -166 -194 -161
    HEART_RATE 54
    LEAD_OFF_STATUS 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0
    -4 -12 -38 -148 -114 -172 -199 -174
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Funny thing, I have the exact same problem.

    As far as I know there is no quick fix to this problem.
    You will have to read each line in as text and parse each line.

    I hope this helps.
     
Loading...