
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
MATLAB - Text Formatting
Text formatting comes into picture when you want to display the text in a specific format.
Matlab makes use of formatting operators to manage with the notation, alignment , significant digits etc. The functions num2str and sprintf are mostly used methods that take care of text formatting using formatting operators.
For example formatting operator like −
%f is used to convert floating point values to text using fixed-point notation.
%.2f is used to represent 2 digits after the decimal point.
%12f is used to show 12 characters in the output , padding and spaces.
In Matlab the functions with formatting operators support are compose, num2str, sprintf, fprintf.
Formatting Operator Fields
There are in total six fields for a formatting operator. Here is the details of it

The formatting operator six fields consists of Identifier, Flags, Field width ,Precision, Subtype and Conversion Character.From the six fields conversion character is the only mandatory param that goes along with the leading % character.
Conversion Character
The conversion character tells about the output notation. It takes a single character and is at the last in the format specifier.
Specifier | Description |
---|---|
c | Single character (char) |
d | Decimal notation (signed) |
e | Exponential notation (using a lowercase e, as in 3.1415e+00). |
E | Exponential notation (using an uppercase E, as in 3.1415E+00). |
f | Fixed-point notation. |
g | The more compact of %e or %f. |
G | This is the same as %g. |
o | Octal notation (unsigned). |
s | Character vector or string array. |
u | Decimal notation (unsigned). |
x | Hexadecimal notation small case (a-f) |
X | Hexadecimal notation upper case (A-F) |
Here is an example using conversion characters wherein will format the number to display in Hexadecimal form.
>> N = 30*ones(1,3); text_format = sprintf('%X', N) text_format = '1E1E1E' >>
To get the decimal and fixed point formatting of the same number you can make use of %d and %f in the sprintf method as shown below.
>> N = 30*ones(1,3); >> text_format = sprintf('%d %f', N) text_format = '30 30.00000030 ' >>
Subtype
Subtype is a single alphabet that just comes before the conversion character.Without subtype the conversion specifiers like %o, %x, %X, and %u will consider the input data given as integers.
So to consider the input data to be floating point instead of integers and to convert them to octal, decimal, or hexadecimal you can consider below subtype specifiers
Specifier | Description |
---|---|
b | For input data that is double precision floating point values. |
t | For input data that is single precision floating point values. |
Example
>> N = 15; >> text_format = sprintf('%bu', N) text_format = '04624633867356078080'
Precision
This field immediately follows a period in the formatting operator. The number used is 0 or a positive integer. For example %6.3f here the precision is 3.
Example
>> text_format = sprintf('%.2f', 1500) text_format = '1500.00' >>
Field Width
Is the positive integer in the formatting operating field width that tells about the number of digits or characters in the output. For example %5.2f , here the field width is 5.
By default you will space padded at the left side , If the field width is greater than the number of characters to be displayed.
Example 1
>> text_format = sprintf('|%3e|%f|%5f|',[33333 45454 50.12123]) text_format = '|3.333300e+04|45454.000000|50.121230|' >>
Example 2
>> text_format = sprintf('%20s', 'Hello world') text_format = 'Hello world' >>
Flags
This field is optional but using it can give you additional formatting to your output. The table below describes the characters you can make use of during the formatting.
Character | Description | Example |
---|---|---|
Minus sign (-) | Left justify the input given | %-3.2d |
Plus sign (+) | If numeric it will show the sign +/- in front of the numeric input data. For text input it will be right justify. |
%+3.2d %+3s |
Space | Insert space before the input given | % 3.2f |
Zero(0) | Use padding before the input given | %03.2f |
Pound Sign (#) | With this specific numeric input can be changed −
|
%#3.0f |
Example 1: Using the minus sign (-)
>> txt_format = sprintf('Example of left-justify: %-12.2f',11.3) txt_format = 'Example of left-justify: 11.30 ' >>
Example 2: Using the plus sign (+)
>> text_format = sprintf('plus sign: %+5.2f',12.3) text_format = 'plus sign: +12.30' >>
Example 3: Using Space for Padding
>> mytxt = sprintf('Padding with zeroes: %010.2f',5.2) mytxt = 'Padding with zeroes: 0000005.20' >>
Example 4: Using Zero(0) for Padding
>> mytxt = sprintf('Padding with zeroes: %010.2f',5.2) mytxt = 'Padding with zeroes: 0000005.20' >>
Identifier
An identifier refers to a name given to a variable, function, or other element in the code. When it comes to text formatting, an identifier can be used to specify the location where a value should be inserted within a formatted string.
To include an identifier in text formatting in MATLAB, you can use the % character followed by a number (or optional name) that specifies the position of the corresponding argument in the input argument list.
Example
>> a = 'My age is: '; b = 25; mystr = sprintf('%s %d', a, b) mystr = 'My age is: 25' >>
Text Formatting using compose() Method
The compose() function in MATLAB takes a format specifier string as its first argument, and one or more values or variables as subsequent arguments. It returns a formatted string by replacing the placeholders in the format specifier with the provided values.
Example
a = 'My age is: '; b = 25; mystr = compose('%s %d', a, b)
On execution in matlab the output is
>> a = 'My age is: '; b = 25; mystr = compose('%s %d', a, b) mystr = 11 cell array {'My age is: 25'} >>
Text Formatting using num2str() Method
The num2str() function in MATLAB is used for converting numeric values to their corresponding string representations. It takes one or more numeric input values and returns a string representation of those values.
The basic syntax for num2str is −
str = num2str(value, format)
- value represents the numeric value or array that you want to convert to a string.
- format (optional) specifies the format of the resulting string. It can be a format specifier like '%d' for integers or '%0.2f' for floating-point numbers, among others.
Example
pi_value = 3.14159; str = num2str(pi_value, '%0.3f')
On execution in matlab you will get
>> pi_value = 3.14159; str = num2str(pi_value, '%0.3f') str = 3.142 >>
Text Formatting using fprintf() Method
The fprintf() function in MATLAB is used for writing formatted data to a file or the command window. It allows you to combine text and data in a flexible and organized manner.
The basic syntax of fprintf() is −
fprintf(format, A1AN)
- format is a string that specifies the format of the output. It consists of static text and format specifiers, such as %s, %d, %f, or %e, that represent the placeholders for the corresponding data in A.
- A1..AN is the data to be written, which can be a scalar, vector, matrix, or cell array.
Example
a = 'My age is: '; b = 25; fprintf('%s %d', a, b)
On execution in matlab you will get
>> a = 'My age is: '; b = 25; fprintf('%s %d', a, b) My age is: 25 >>