Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Program stops after failing to locate file. I Need Help with Why.

Asked by Adam Bourke on 16 Jan 2013

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?

Thanks, Adam

THE CODE:

	
        ...
        ...
        filename = sprintf('Densities%03s.mat', num2str(ID));
	disp('Finding Densities')
	disp(sprintf('\tSearching for Densities as "%s"', filename))
	debug=1 %DEBUG
	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);
                ...
                ...

1 Comment

Image Analyst on 16 Jan 2013

Is calcdens() the same as calcdens_d()? Assuming so, I don't see why the print lines should go after calcdens_d. Why don't they happen before calcdens_d?

Also, you don't need to do it that weird way, because disp(sprintf) is redundant. Just use fprintf:

fprintf('calcDense:\n') %DEBUG
fprintf('\tNo Densities file found\n\tCalculating Densities');
Adam Bourke

Products

1 Answer

Answer by Walter Roberson on 16 Jan 2013

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.

0 Comments

Walter Roberson

Contact us