looking for an efficient way to activate all fprintf in the function
    조회 수: 3 (최근 30일)
  
       이전 댓글 표시
    
I have a function and into it there are a lot of fprintf that print out some informations.I would create a way to show or not show this informations using only one flag.
for example
debug = 0;
if debug ~= 0
 fprintf(...);
end
Since I have too many fprintf in the function I don't want write if every time but I would activate and deactivate fprintf once time for all printf
댓글 수: 0
채택된 답변
  Daniel Shub
      
      
 2012년 9월 28일
        
      편집: Daniel Shub
      
      
 2012년 9월 28일
  
      If you are using a function, you can overload fprintf with a nested function.
function myfunction(debug)
  fprintf('first one\n');
  fprintf('second one\n');
  function fprintf(varargin)
    if ~debug
      builtin('fprintf', varargin{:});
    end
  end
end
you can then call with myfunction(true) or myfunction(false)
댓글 수: 0
추가 답변 (1개)
  Dr. Seis
      
 2012년 9월 28일
        Probably not what you are looking for, but a quick way may be to just do a search and replace all (i.e., search for: "fprintf" replace with: "%fprintf") before running. Then do the opposite when you don't want debug mode. The "%" will comment out the fprintf line (unless you have multiple lines associated with your fprintf).
The other way may be to create your own fprintf function (e.g., "myfprintf") and have it accept your flag to print or not to print as one of its' arguments.
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


