Code covered by the BSD License  

Highlights from
printf

image thumbnail

printf

by

 

18 Oct 2012 (Updated )

An improved print command with status and timing functionality

printf - An Improved Print Command With Status and Timing Functionality

printf - An Improved Print Command With Status and Timing Functionality

Falko Schindler, August 6, 2012

Contents

Print formated data to the command line

Calling printf with one string argument only is equivalent to using the disp command.

printf('Hello World');
Hello World 

Additional arguments are included in the output like they are with fprintf.

printf('%d + %d = %d', 1, 1, 1 + 1);
1 + 1 = 2 

Timing

Calling printf with a string ending with three dots '...' will start timing the following commands. Only calling printf without any arguments will stop the time and print the elapsed time.

N = 1000;
printf('Inverting a %dx%d matrix...', N, N);
inv(rand(N));
printf;
Inverting a 1000x1000 matrix .................................... 0.175 sec

Very long lines are trimmed in order to have uniformly sized lines.

printf(['This is a very very long line ' ...
    'with lots of information about the ongoing computations']);
printf(['This line introduces an action that is timed: ' ...
    'Thus we should leave enough space for the number of seconds...']);
pause(0.1);
printf;
This is a very very long line with lots of information about the ongoing...
This line introduces an action that is timed: Thus we should..... 0.100 sec

Hierarchical actions

Actions are displayed hierarchically if multiple titles follow each other without printing times in between.

printf('A timed group of actions...');
for i = 1 : 3
    printf('Action %d...', i);
    pause(0.1);
    printf;
end
for i = 4 : 6
    printf('Action %d (no time)', i);
    pause(0.1);
end
printf;
A timed group of actions 
    Action 1 .................................................... 0.100 sec
    Action 2 .................................................... 0.100 sec
    Action 3 .................................................... 0.100 sec
    Action 4 (no time) 
    Action 5 (no time) 
    Action 6 (no time) 
0.604 sec

Status

Calling printf with one or two scalars prints the current status as percentage. This number later is overwritten with the elapsed time. Passing two arguments, e.g. the current iteration and the total number of iterations, will only print a number if the percentage changed since the last call which can reduce the number of prints to the command line significantly. This check is not possible with one argument only.

N = 900;
printf('Inverting %d matrices (%dx fprintf)...', N, N);
for n = 1 : N
    inv(rand(100));
    printf(n / N);
end
printf;
printf('Inverting %d matrices (100x fprintf)...', N);
for n = 1 : N
    inv(rand(100));
    printf(n, N);
end
printf;
Inverting 900 matrices (900x fprintf) ........................... 0.807 sec
Inverting 900 matrices (100x fprintf) ........................... 0.694 sec

Settings

Printing a hierarchy of 3 actions initially looks as follows.

printf('Three-levels of actions...');
for i = 1 : 2
    printf('Action %d...', i);
    for j = 1 : 3
        printf('Sub-action %d.%d...', i, j);
        pause(0.1);
        printf;
    end
    printf;
end
printf;
Three-levels of actions 
    Action 1 
        Sub-action 1.1 .......................................... 0.100 sec
        Sub-action 1.2 .......................................... 0.100 sec
        Sub-action 1.3 .......................................... 0.100 sec
    0.302 sec
    Action 2 
        Sub-action 2.1 .......................................... 0.100 sec
        Sub-action 2.2 .......................................... 0.100 sec
        Sub-action 2.3 .......................................... 0.100 sec
    0.302 sec
0.605 sec

We define a new line width of 60, choose an indentation of 2 characters and set a maximum depth of 2.

printf('!set', 'width', 60, 'indent', 2, 'depth', 2);

The very same actions now yield the following output.

printf('Three-levels of actions...');
for i = 1 : 2
    printf('Action %d...', i);
    for j = 1 : 3
        printf('Sub-action %d.%d...', i, j);
        pause(0.1);
        printf;
    end
    printf;
end
printf;
Three-levels of actions 
  Action 1 ....................................... 0.302 sec
  Action 2 ....................................... 0.302 sec
0.605 sec

Let's restore all default settings.

printf('!reset');

Clearing the print stack

When developing a piece of software, you might have stopped the program during one action. Now the concluding printf command is missing and all following output will be printed with additional indentation. The printf function can not determine whether there is still a complicated action is going on or if the program stopped completely. To fix this issue you can call printf with a '!clear' command that will clear the stack of print commands.

printf('Some action with timing that is interrupted...');
printf('Output with too much indentation');
printf('!clear');
printf('Output with correct indentation');
Some action with timing that is interrupted 
    Output with too much indentation 
Output with correct indentation 

printf and diary

By skipping status outputs and thus avoiding backspaces '\b' printf also renders nicely in diary log files.

diary diary.log;
printf('Counting from 1 to 10...');
for i = 1 : 10
    printf(i, 10);
    pause(0.1);
end
printf;
diary off;
Counting from 1 to 10 ........................................... 1.008 sec

Here is the content of diary.log.

pause(1);
type diary.log;
delete diary.log;

Contact us