how do I read a continuously changing file in matlab

Discussion in 'MATLAB' started by Sriram Rallabhandi, Jan 22, 2004.

  1. hello,

    I need to read from a file which is changing continuously. Its a text
    file in which data is being written as my application is run. After
    every few iterations, a data row is written to this text file, which I
    then need to read and plot. The time it takes to add a new row to the
    file varies with the input file.

    I have tried the following:

    if exist(fname,'file')
    fid = fopen(fname,'r');
    r1=1;
    while (timeelap < 20)
    if (feof(fid))
    pause(1);
    timeelap = timeelap +1;
    else
    M = str2num(fgetl(fid));
    if (~isempty(M))
    timeelap =0;
    iter(r1) = M:),1);
    RMSresid(r1) = M:),2);
    plot(Iter,RMDresid,'erasemode','none');
    drawnow;
    r1=r1+1;
    end
    end

    however, it does not work as the file is being changed. So, I changed
    the code to do fclose(fid) when feof(fid) is true, pause(1), then
    fopen(fname,'r') and position my cursor at the appropriate position.
    this does not work either.

    Any help is appreciated.

    Thanks
    Sriram
     
    Sriram Rallabhandi, Jan 22, 2004
    #1
    1. Advertisements

  2. Sriram Rallabhandi

    Dan Hensley Guest

    I think the solution you need is going to be more complicated than what
    you have. You'll probably want to use a timer object to periodically
    do the update. As far as checking the file, here's what I'd do.

    Just before opening it, do a dir on it to get the filesize. Then open the
    file, read data out of it up to the filesize, then close it. Update your
    plot. Next time you come to update your plot, check the filesize again.
    Now you know how much has been written to it. Open the file and use fseek
    to jump to the last written place + 1 (the previous filesize + 1). Read
    the next bit of data out and close the file. Keep repeating this.

    Here are the functions you may need
    timer
    fseek
    ftell
    dir

    Dan
     
    Dan Hensley, Jan 22, 2004
    #2
    1. Advertisements

  3. Thanks Dan, it did help a little. However, I'm not completely done.
    Suppose, my file gets updated 3 times. So, i have made my numtasks=3
    which is supposed to call myfun 3 times. first time, it shud go to end
    of file, and plot. then wait 20 seconds and look in the file again.
    however, it only goes in myfun just once.

    any suggestions?

    clear all;
    global pos1 pos2 fsize1 fsize2 r1 r2 numtasks;
    r1=1;
    r2=1;
    figure;
    clf;hold on;
    fsize1 = 0;
    fsize2 = 0;
    pos1=0;
    pos2=0;

    numtasks = 3.0;

    t = timer('TimerFcn',@myfun,'Period',20.0,'StartDelay',1.0,'TasksToExecute',numtasks);
    start(t);


    function myfun(t,event)
    global pos1 pos2 fsize1 fsize2 r1 r2 numtasks;

    fname1 = 'residn.dat';
    fname2 = 'loadsn.dat';

    flag1 =0;
    flag2 =0;

    N1 = dir(fname1)
    N2 = dir(fname2)

    if (N1.bytes > fsize1)
    fid1 = fopen(fname1,'r');
    tline1 = fgetl(fid1);
    fseek(fid1,pos1+1,-1);
    flag1 =1;
    end
    if (N2.bytes > fsize2)
    fid2 = fopen(fname2,'r');
    tline2 = fgetl(fid2);
    fseek(fid2,pos2+1,-1);
    flag2 =1;
    end

    if (flag1 && flag2)

    while (~feof(fid1))
    M1 = str2num(fgetl(fid1));
    if (~isempty(M1))
    Iter(r1) = M1:),1);
    RMSresid(r1) = M1:),2);
    NormRMS(r1) = M1:),3);
    MaxRMS(r1) = M1:),4);
    subplot(3,1,1),
    semilogy(Iter,RMSresid,'erasemode','none');
    drawnow;
    r1 = r1+1;
    clear M1;
    end
    end
    pos1 = ftell(fid1)
    fclose(fid1);
    while (~feof(fid2))
    M2 = str2num(fgetl(fid2));
    if (~isempty(M2))
    Iter2(r2) = M2:),1);
    CD(r2) = M2:),2);
    CL(r2) = M2:),3);
    subplot(3,1,2), plot(Iter2,CD,'erasemode','none');
    drawnow;
    subplot(3,1,3), plot(Iter2,CL,'erasemode','none');
    drawnow;
    r2 = r2+1;
    clear M2;
    end
    end
    pos2 = ftell(fid2)
    fclose(fid2);
    end

    fsize1 = N1.bytes
    fsize2 = N2.bytes


    Thanks
    Sriram
     
    Sriram Rallabhandi, Jan 22, 2004
    #3
  4. Sriram Rallabhandi

    Dan Hensley Guest

    Are you sure it only executes myfun once, or is it possible it doesn't
    recognize that the file size hasn't changed? Of the top of my head I'm
    not sure why it only executes once. You could try removing that condition
    from the timer to make sure it actually will update more than once. If it
    does, then another possibility is to set a condition in myfun that says
    that if the filesize does not change in 2 calls, to have it shut down the
    timer.

    By the way, you can use persistent variables instead of global to store
    some or all of the quantities you need to store between calls.

    Dan
     
    Dan Hensley, Jan 22, 2004
    #4
  5. I'm wondering if polling the archive attribute bit and resetting it after
    you read the change would be of any help.

    Scott
     
    Scott Seidman, Jan 22, 2004
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.