I'm writing a code that needs to do quite a large task, and then save the result as a .mat file, so it can be loaded when the program is run again, instead of doing it every time.
I use if ~exist() to check if that file exists. When I run the program for the first time, the file doesn't exist, and so the stuff in the if statement should run. However after it prints out "Searching for Densities as filename" (The third line in the snippet below) the program just stops producing output. I added the three lines marked %DEBUG to try and see what was going on. It prints both debug = 1 and debug = 2, which implies that the if codeblock is entered fine. However, it doesn't print "calcDense:" (the third DEBUG line), or anything after that - not does it return control of matlab to the user. Any ideas? (I'm using MATLAB from the command line with -nodesktop)
EDIT. The question as written is no longer accurate - the program does continue to run. However, The text displays (calcDense:, No Densities File Found, Calculating Densities) do not appear until after the long silent calcdens function completes. I assume this is a buffering issue ,although this may be wrong. Does anyone know how to solve this?
... ... filename = sprintf('Densities%03s.mat', num2str(ID)); disp('Finding Densities') disp(sprintf('\tSearching for Densities as "%s"', filename))
if ~exist(filename, 'file') debug=2 %DEBUG disp(['calcDense:']) %DEBUG
% Calculate smoothing radii disp(sprintf('\tNo Densities file found')) disp(sprintf('\tCalculating Densities')) [h,dn]=calcdens_d(r,Nsph,h_guess); dn=dn/length(r); ... ...
r2012b has, I gather, changed buffering for disp() and fprintf(1).
Before that, anything disp() or fprintf(1) is subject to arbitrarily long buffering. In theory there is a way to overcome that with fseek() but I have not been able to establish that the method works in MATLAB.
Try, instead of disp(), fprintf() to file id 2 (stderr), which should not be buffered.