2

I have a huge data set that I'm caching, then writing filtered analysis data to disk. I have various disp() commands in my code, along with fprintf() calls.

I'd like to see the results both in the file, and on the screen while the processes are running, but what I'm finding is that I get nothing until I terminate the program, at which point all the data is written into my file and the disp() floods the terminal.

Would there be a way to force disp() and the fprintf() to execute as they're being processed??

Here's an example:

function one(varargin)
    setenv GNUTERM 'x11';

    dirname = strcat(pwd, '/fileset');
    files = dir(dirname);
    disp('reading directory'), disp(dirname);
    fileidx = find(~[files.isdir]);
    out = fopen('write_data.txt', 'w');
    fprintf(out, '"--- var a[0]", "--- var [1]";\n');

    numfiles = length(fileidx);
    for i = 1:numfiles
        dispstring = sprintf('processing file %d of %d...', i, numfiles);
        disp(dispstring);
        filename = [dirname, '/', files(fileidx(i)).name];
        disp(filename);
        fid = fopen(filename, 'r');

        %some processing here to obtain timevalues and maxvars

        for i = 1:length(timevalues)
            fprintf(out, '%d, %d;\n', timevalues(i), maxvars(i));
        end 
    end 

    fclose(out);
end

I saw this post, but I wasn't sure which of the methods suggested applied to me. It also seemed like fflush() was meant for pushing data into a plot at higher priority.

Community
  • 1
  • 1
jml
  • 1,745
  • 6
  • 29
  • 55
  • where would you put the pause? i was thinking at the end of the for loop that prints... – jml Feb 07 '13 at 04:52
  • If you had posted it as a solution, I could confirm it. Aside from that, I need more details on what you would do. Feel free to post an actual answer... Thx – jml Feb 12 '13 at 18:47
  • Though someone already upvoted it I guess I just had to delete my answer (`pause` didn't help). Don't know what else could do the trick, perhaps switching to Matlab? – Dennis Jaheruddin Feb 19 '13 at 09:01
  • Switching to Matlab is not an option due to the difference in price (free vs. thousands of dollars). – jml Feb 25 '13 at 20:38

1 Answers1

1

I have had this problem before and you do you fflush to solve it. Write

fflush(stdout);

to force the terminal to be updated with the results of all the prints and disps to stdout that came before the call to fflush(stdout). I'm not sure if you should bother flushing the output to the file as it will probably make your code slower, but if you want to you can do

fflush(out);
mfbutner
  • 141
  • 6
  • thanks. I will be checking this out within a day or so; I really appreciate your input and have a feeling that this may in fact work out. BTW- would I use *both* `fflush(stdout)` and `fflush(out)` ? – jml Mar 03 '13 at 19:54
  • fflush(stdout) to force the terminal to be updated with results and fflush(out) to force the update to your file. You would have to do both if you want both the terminal and file to be updated inside the for loop. – mfbutner Mar 03 '13 at 20:03