Reputation: 1806
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.
Upvotes: 2
Views: 419
Reputation: 141
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);
Upvotes: 1