Take the 2-minute tour ×

I have created a simulation in matlab. It is a script file where I have a main for cycle and inside it, I have written some code and functions. I have used fprintf function to debug my code during the development to check if everything go well. When I run my simulation instead I would not execute fprintf because It needs so many time to execute fprintf since my for cycle is executed 1E6. I would a way to decide when execute fprintf and when not.

share|improve this question
add comment

1 Answer

Two options:

1) include some variable called "debug" for example, and put every fprintf() in:

if (debug==1):
    fprintf(blah blah)

then you just need to change the debug variable at the beginning of your program when debugging, or you can even get it as input from the user (a flag).

2) wrap fprintf in a function, and then just use that function instead, something like:

function myfprintf(x)
   global debug

   if (debug==1):
share|improve this answer
I thought to this method but according to profiler, my simulation spent so many time checking in if clause –  Mazzy Oct 14 '12 at 20:32
@Mazzy Are you sure you are interpreting the profiler results correctly? doing an if() is relatively fast, it will usually only be a bottleneck if the rest of your code is highly optimized. –  Bitwise Oct 14 '12 at 20:38
@Mazzy What version of MATLAB are you using? That really surprises my that an if statement with a condition that always evaluates to false would be a bottleneck, as that seems like something that would be right up the JIT accelerator's alley. I'm wondering if somehow the profiler is actually confounding the results. If you just timed the overall execution of your code using tic and toc what is the difference between code with if(debug==1) around your fprintf's (where debug=0) and code that just has your fprintf's commented out (ie. the best case scenario as far as runtime is concerned) –  grungetta Oct 15 '12 at 4:35
add comment

Your Answer


