As an experienced MATLAB developer, being able to generate clearly formatted textual output is a must. The fprintf function is the sledgehammer in your toolbox, giving absolute control over the appearance, styling and precision of printed numbers, tables, logs and more. This guide takes you from basic usage to advanced tips and tricks for maximizing productivity.

fprintf Basics

The syntax is simple, flexible and consistent:

fprintf(fileID, formatting, data1, data2, ...)

Where fileID specifies the output destination, formatting defines how data appears and data1, data2, ... are the values to print.

fileID can refer to an open file from fopen, but defaults to 1 (command window).

The real power comes from string format specifiers within the formatting string:

Specifier Data printed Example
%d Integers %d
%f Floating point %2.2f
%s Strings %30s
%% Literal % %%

Let‘s see some examples:

Printing Text and Variables

Mix text with live variable data:

x = 5; y = 10;
fprintf(‘x = %d, y = %d\n‘, x, y) 

>> x = 5, y = 10

Easy! The \n adds a newline.

Floating Point Precision

Set float precision by specifying digits after decimal:

z = 1.23456;
fprintf(‘z = %.3f\n‘, z)

>> z = 1.235

Add padding to neatly align columns:

fprintf(‘%-10s %10.3f\n‘, ‘Label‘, z);

>> Label 1.235

Formatting Tables

Print matrices as formatted tables with spacing control:

M = [3 5 7; 9 2 4];

fprintf(‘%10s %10s\n‘, ‘X‘, ‘Y‘);
fprintf(‘%10.4f %10.4f\n‘, M);    

>>           X           Y
         3.0000     5.0000
         9.0000     2.0000

Professionally style your results!

Date and Time Formatting

MATLAB datetimes can be printed as formatted strings like so:

dt = datetime(2023, 3, 15, 13, 45, 30);
fprintf(‘Time: %s\n‘, datestr(dt));

>> Time: 15-Mar-2023 13:45:30  

Many creative formats are possible – see full reference.

Logging with Timestamps

Prefixing fprintf output with timestamps creates readable logs:

timestamp = datestr(now,‘HH:MM:SS‘);
fprintf(‘%s - Started simulation\n‘, timestamp); 

>> 15:11:23 - Started simulation

Log progress events, handle errors neatly.

Scientific Notation

For very large/small numbers, use exponential formats:

hugeNum = 1.23456e10;
tinyNum = 1.23456e-5; 

fprintf(‘%e\n‘, hugeNum);
fprintf(‘%e\n‘, tinyNum);

>> 1.234560e+10
>> 1.234560e-05

Set precision with .NFe where N pads left side zeroes:

fprintf(‘%10.3e\n‘, tinyNum)  

>> 1.235e-05

Writing Data to File

Redirect output to a file with fopen:

fileID = fopen(‘data.txt‘,‘w‘);
x = 5;
fprintf(fileID, ‘x = %d‘, x);
fclose(fileID);

This saves x=5 to data.txt. Add ‘a‘ file open mode to append.

💡Tip: For large datasets use dlmwrite for faster performance

Limitations

fprintf has some limitations to be aware of:

  • Cannot directly print cell arrays, structs, user objects

  • Output filetype always text – for binary use fwrite

  • File writing can be slow for big data – see alternatives mentioned

Conclusion

This guide has only scratched the surface of the versatility of MATLAB‘s fprintf function. By harnessing format strings and controlling precision, spacing, padding and more, you can move beyond basic printing to creating publication quality tables and logs. The key is realizing how creative you can be in defining output formats to best communicate your data for analysis and readability.

So don‘t settle for blank command window output! Take your results and logs to the next level with fprintf.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *