Reputation: 1965
At the begining of each of my function I pass a debug flag that is either true or false. I would like to create a function (possibly inline) that uses this flag and prints only if the value is set to true. e.g.:
fprintff = @(str) debug&fprintf(str)
...
...
fprintff(str); %will only print if debug is set to true
I know I can do it with fprintff(str,debug)
, but I want to use it without it.
No persistent or global!
Upvotes: 0
Views: 133
Reputation: 22284
One possibility is to define the debug print function to rely on the short-circuiting of the &&
operator. This way fprintf
is only evaluated when debug_flag == true
at the moment the function is defined.
function foo(bar,debug_flag)
debug_print = @(varargin) debug_flag && fprintf(1,varargin{:});
...
debug_print('This only prints if debug_flag == true\n');
...
end
Upvotes: 1