Demystifying Printing in MATLAB: From Simple Outputs to Polished Figures
Tags: matlab
Published: 2024-05-20 10:32:04

MATLAB offers a versatile environment for numerical computations, visualization, and data analysis.  But how do you effectively present your results? This guide explores various methods for printing in MATLAB, catering to both basic output display and professional-looking figure generation.

**1. Direct Display: A Straightforward Approach**

The most effortless way to print variable values is by simply typing the variable name in the Command Window without a semicolon ( ; ) at the end.  MATLAB will evaluate the variable and display its contents directly. This approach is suitable for quick checks and inspecting small data sets.

For instance:

`x = 5;
y = 3.14;
z = [1 2 3];

x
y
z
`

This code will print the value of x (5), y (3.14), and the elements of vector z (1, 2, 3) on separate lines in the Command Window.

[Mastering Edison Robots: A Comprehensive Guide to Building and Programming Your Own Robots](https://www.amazon.com/dp/B0CTTVTWLT)

**2. Unveiling the Power of disp: Clarity and Control**

The disp function provides more control over the printing process. It takes one or more arguments, each representing the data you want to display. Unlike direct display, disp doesn't print variable names by default.

Here's an example:

`disp('The value of x is:')
disp(x)
disp(['Pi is approximately: ' num2str(y)])
`

This code first prints a message "The value of x is:", followed by the value of x on a new line. The num2str function converts the numerical value of y to a string before printing it with a descriptive message.

Key features of disp:

Formatting: You can combine strings and variables within disp for informative output.
Newlines: Use disp with an empty string argument (disp('')) to insert a newline for better readability.
Multiple Arguments: disp can handle multiple arguments, printing them sequentially on separate lines.

**3. Precision and Formatting with fprintf: The C-style Approach**

For intricate formatting requirements, fprintf comes into play. Borrowing syntax from the C programming language's printf function, fprintf offers a powerful way to control how data is displayed.

It takes two mandatory arguments:

Format String: A string containing text and special format specifiers (starting with %). These specifiers define how variables are incorporated into the output.
Variables: A list of variables corresponding to the format specifiers in the format string.

Here's a basic example:

`a = 123.4567;
fprintf('Value of a: %f\n', a)
`
The format specifier %f represents a floating-point number.  This code prints "Value of a: 123.4567" (by default, with 6 decimal places). You can adjust the number of decimal places using a modifier after the %:

`fprintf('Value of a (2 decimals): %.2f\n', a)
`

This will print "Value of a (2 decimals): 123.46".

Exploring Format Specifiers:

%d: Signed decimal integer
%c: Single character
%s: String
%e or %g: Scientific or general notation for floating-point numbers
Additional Features:

Newline Character: Include \n within the format string for newlines.
Tabs: Use \t for horizontal tab spacing.
Caution: Ensure the number of format specifiers matches the number of variables provided to fprintf.

**4. Printing Figures for Visual Impact**

MATLAB excels at creating informative figures. To print a figure, use the print function. It offers various functionalities, depending on the arguments provided.

Basic Printing: print by itself sends the current figure to the default system printer.

Specifying Filename and Format: print('filename', 'format') saves the figure as a file with the specified format (e.g., .png, .jpg, .eps).
Targeting Specific Printers: print('-Pprinter_name') sends the figure to a particular printer.
Additional Options:

Resolution Control: Use options like '-r300' to set the output image resolution (300 dpi in this case).

Color/Grayscale: Control color output with options like '-dpsc2'.
Remember: Ensure you have a working printer connected to your system for direct printing.