views:

273

answers:

4

I use anonymous functions for diagnostic printing when debugging in MATLAB. E.g.,

debug_disp = @(str) disp(str);
debug_disp('Something is up.')
...
debug_disp = @(str) disp([]);
% diagnostics are now hidden

Using disp([]) as a "gobble" seems a bit dirty to me; is there a better option? The obvious (?) method doesn't work:

debug_disp = @(str) ;

This could, I think, be useful for other functional language applications, not just diagnostic printing.

+3  A: 

If you're simply looking for a "do-nothing" command to replace the body of the anonymous function, I'd probably go with DRAWNOW:

debug_disp = @(str) drawnow;

This will simply flush the event queue and update the graphics instead of displaying any text.

gnovice
`drawnow()` events incur a performance hit if you have open graphics windows.
shabbychef
@shabbychef: If you don't want to update graphics, you can use `drawnow('UPDATE')`.
gnovice
+4  A: 

I think disp([]) or disp('') is perfectly acceptable. It doesn't return anything and it has no side effects.

Steve Eddins
A: 

Here's a do-nothing anonymous function. It does nothing, and returns an empty array, which you can just ignore. You'll need to suppress disp by putting a semicolon after it.

debug_disp = @(str) [];

The disp([]) should work fine too. Whichever style you prefer.

Andrew Janke
A: 

try debug_disp = @(str)(1);

shabbychef

related questions