option to stop printing the results in an output file

조회 수: 3 (최근 30일)
Alaa
Alaa 2013년 3월 3일
Hi! I am working on a matlab code that contains many functions. In each mfile I inserted a fprintf to print the results in a file that I called output file. I want to run some simulations but I must insert in all the .m files an option to not print out the output in that file (else with many simulations it won't work to keep adding to the output file). What option must I add? I appreciate your fast reply!
Best regards, Alaa
  댓글 수: 1
Jan
Jan 2013년 3월 4일
편집: Jan 2013년 3월 4일
"Fast reply"?! We are volunteers. Any pushing is futile.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2013년 3월 4일
You can create a dedicated function for the output:
function myFprintf(varargin)
persistent FID
cmd = vargargin{1};
if strcmp(cmd, '$open') % Or what ever
FileName = varargin{2};
FID = fopen(FileName, 'w');
if FID == -1
error('Cannot open file: %s', FileName);
end
fprintf(1, '==> File opened for writing: %s\n', FileName);
return;
elseif strcmp(cmd, '$close') % Or what ever
if FID ~= 0
FileName = fopen(FID);
fprintf(1, '==> Close file: %s\n', FileName);
fclose(FID);
end
FID = 0;
return;
end
if FID ~= 0
fprintf(FID, varargin{:});
else
fprintf(1, varargin{:}); % Write to command window instead?!
end
Now a leading myFprintf('$open', fullfile(Folder, File)) opens a file an all following calls are written to this file. But myFprintf('$close') closes the file and subsequent calls avoid writing to the file.
Now you have to find and replace the existing fprintf calls to myFprintf (or a nicer name...).

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by