Written and maintained by CASRAI Editorial Board
Last updated
Who this is for: researchers and analysts who need to show text, variables, matrices, tables or more decimals in the MATLAB Command Window, and who want to know which function to reach for. After reading you should be able to pick between disp, fprintf and simply typing a variable name, and control how many digits appear.
Short answer: to print text or a value to the Command Window use disp(X) (no variable name shown) or fprintf(formatSpec, A1, ..., An) (full control of layout). To print more decimals, use format long for the display or a format such as %.10f in fprintf. The function print is a different tool: it sends a figure to a printer or saves it to a file.
What printing is used for in research
Analysts print to the Command Window to check intermediate results, report progress inside a loop, and produce log lines that can be redirected to a file. MATLAB offers several routes that behave differently, so the choice matters for reproducible output. The table below maps the goal to the function; each row is taken from the MathWorks reference pages listed under Sources.
| Goal | Use | What the documentation says |
|---|---|---|
| Show a value without its variable name | disp(X) |
Displays the value of X without printing the variable name |
| Show a value with its name | Type the variable name (no semicolon) | The output starts with X = |
| Print formatted text and numbers | fprintf(formatSpec, A1, ..., An) |
Applies the format to the data and displays it on screen |
| Write the same formatted text to a file | fprintf(fileID, formatSpec, ...) |
Writes to the file identified by fileID (from fopen) |
| Change digits shown for all numbers | format long, format short, etc. |
Affects only how numbers appear, not how they are computed or saved |
| Print or save a figure | print(filename, formattype) |
Prints a figure or saves it to a file in a specific format |
Install or access
All functions on this page are part of core MATLAB and need no toolbox. Obtain MATLAB only from MathWorks (mathworks.com) or through your institution’s licence. Licensing terms and costs are set by MathWorks and your institution, so consult them directly.
Step-by-step: print in MATLAB
How to print to the console in MATLAB (text and strings)
Call disp with a character vector or string, or use fprintf and end the format with n so the next output starts on a new line.
disp('Hello World.')fprintf('Iteration finishedn')
In fprintf, n is the new-line character. If the text file will be opened in Windows Notepad, the documentation recommends rn.
Print a variable in MATLAB
Three routes give three layouts. With A = [15 150], typing A (no semicolon) prints A = followed by the values; disp(A) prints just 15 150; fprintf('%dn', A(1)) prints only the first element under your own format. Note that disp prints nothing for an empty array.
A format specification starts with %. The ones used most often in research scripts are:
| Specifier | Prints |
|---|---|
%d, %i |
Signed integer |
%u |
Unsigned integer |
%f |
Fixed-point notation |
%e, %E |
Exponential notation |
%g, %G |
The shorter of %e or %f, without trailing zeros |
%c |
Single character |
%s |
Character vector or string |
Flags go between % and the letter: - left-justifies, + always prints a sign, 0 pads with zeros. A number before the letter is the minimum field width. After a decimal point, the number sets digits after the point for %f and %e, and significant digits for %g.
How to print out a matrix in MATLAB
Typing the matrix name prints it with its layout. With fprintf, remember that the format is applied to the elements of the array in column order, and the format repeats until the data run out. That means a matrix prints down its columns unless you transpose it.
A = [1 2; 3 4];fprintf('%d %dn', A) % prints "1 3" then "2 4" (column order)fprintf('%d %dn', A.') % prints "1 2" then "3 4" (row by row)
The second form uses the transpose A.' so each row of A becomes one printed line. For creating and reshaping matrices see Matrix in MATLAB, and for printing inside repeated steps see loops in MATLAB.
How to print out a table in MATLAB
MATLAB tables are displayed automatically: show a table T by typing its name without a semicolon, which prints a header such as T = 5x4 table followed by the column names and rows. Row names, when set, appear as the leftmost column; summary(T) gives an overview. Name the columns when you create the table:
lat = [10; 20]; lon = [30; 40];T = table(lat, lon, VariableNames=["Latitude" "Longitude"])
See MATLAB data tables for building and importing them. To print selected columns with your own layout, pass them to fprintf as shown for matrices.
How to print more decimals in MATLAB
There are two independent controls. format long changes the display of every number: for pi the default format short shows 3.1416, while format long shows 3.141592653589793. Other styles include shortE, longE, shortG, longG, bank (3.14), rational (355/113) and hex. The documentation states that numeric formats affect only how numbers appear in the display, not how MATLAB computes or saves them, so changing the format never changes your data.
For a single line instead of a session-wide setting, give the precision in fprintf:
fprintf('%.10fn', pi) % 3.1415926536
Output and how to read it
dispand typing a name show values for reading;fprintfshows exactly the text you specify, so nothing appears unless you include a new-line.fprintfwith a file identifier writes to a file; identifier 1 is standard output (the default) and 2 is standard error, so messages sent to 2 are kept separate from normal output.nbytes = fprintf(___)returns the number of bytes written.- If a matrix prints in a surprising order, check column order and transpose.
Troubleshooting
- Output has no line breaks: add
nto the format. - Numbers show too few digits: use
format longor a precision such as%.10f. - A matrix prints transposed: use
A.'with a row-wide format. - You wanted paper or an image file: use
print(filename, formattype), for exampleprint("BarPlot","-dpng"); see MATLAB graph and plot for creating the figure.
Free alternatives
| Tool | Printing approach |
|---|---|
| GNU Octave | Free, MATLAB-compatible language that also provides disp and fprintf |
| Python | Built-in print() and formatted string literals (f-strings) |
FAQ
How to print out a matrix in MATLAB?
Type its name without a semicolon, use disp(A), or use fprintf with A.' to print row by row.
How to print more decimals in MATLAB?
Run format long for the display, or use a precision in the format, for example fprintf('%.10fn', x).
How to print to the console in MATLAB?
Use disp('text') or fprintf('textn').
How to print out a table in MATLAB?
Type the table variable name without a semicolon; the Command Window shows the column names and rows.







