comment lines at the beginning of code execution

5 ビュー (過去 30 日間)
Salvatore Mazzarino
Salvatore Mazzarino 2012 年 10 月 14 日
I'm finding a way to comment every line where fprintf function appears if the value of DEBUG variable is setted to 0. for example:
if DEBUG == 0
comment all fprintf lines in my code
end
start the coding...
  1 件のコメント
Salvatore Mazzarino
Salvatore Mazzarino 2012 年 10 月 14 日
When you do simulation with matlab, writing scripts, which technique you used to choose when print out infos and when not print out infos?

サインインしてコメントする。

回答 (1 件)

Image Analyst
Image Analyst 2012 年 10 月 14 日
編集済み: Image Analyst 2012 年 10 月 14 日
I just set a flag and then put all fprintf's and msgbox's inside an if:
showDebugMessages = true; % or false.
....
if showDebugMessages
fprintf('About to run this section of code......\n');
end
...
if showDebugMessages
message = sprintf('The value is %f', value);
uiwait(msgbox(message));
end
  2 件のコメント
Salvatore Mazzarino
Salvatore Mazzarino 2012 年 10 月 14 日
編集済み: Salvatore Mazzarino 2012 年 10 月 14 日
Ok I understood what you want to say indeed I created this function:
function custom_fprintf(debug,varargin)
%FPRINTF Summary of this function goes here
% Detailed explanation goes here
if debug ~= 0
builtin('fprintf', varargin{:});
pause(0.5);
end
end
but when I execute my simulation with DEBUG setted to 0, profiler showed me that my simulation spent so many time checking if clause since my function is into a for cycle executed for 1E6.
for t = 1:1E6
...
custom_fprintf(DEBUG,...);
...
end
I need a way more efficient
Image Analyst
Image Analyst 2012 年 10 月 14 日
Put the if outside the huge for loop. Of course this requires two for loops, one inside the if that has the fprintf, and in the "else" that does not check. But I can't see why you'd want to print something a million times anyway - talk about taking a long time. And how would you ever inspect all of them?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by