Text Formatting in MATLAB
Last Updated :
19 Mar, 2022
In the real-world data can be any form. This data may be in binary, numeric, text, array, and so on. And this data can be converted into text. In Matlab, the text can be formatted using a formatting operator along with formatting functions such as sprintf, numstr, fprintf, compose. These functions/operators control the text notation, significant digit, text alignment, and so on.

In this article, we will see, how you can format the text in MATLAB.
A formatting operator has six criteria that include a numeric identifier, flags, field width, precision, subtype, and a conversion character. In the figure there is space between the operator but while writing code, make sure there are no space characters allowed in the operator. Out of the six field conversion character is only the required field which means it cannot be ignored while formatting text in Matlab. And this conversion character must start by a % percent sign.
Fields of Formatting Operator:
Conversion Character: Just like C programming, Matlab requires a format specifier so that it specifies the notation of the output. The format specifier and meaning is given below:
S.No. |
Format Specifier |
Meaning |
1. |
c |
Single character(char) |
2. |
d |
Decimal notation (signed) |
3. |
u |
Decimal notation (unsigned). |
4. |
e |
Exponential notation (using a lowercase e, as in 3.1415e+00). |
5. |
E |
Exponential notation (using an uppercase E, as in 3.1415E+00). |
6. |
f |
Fixed-point notation |
7. |
x |
Hexadecimal notation (unsigned, using lowercase letters a–f). |
8. |
g |
The more compact of %e or %f |
9. |
G |
Similar to %g |
10. |
o |
Octal notation (unsigned). |
11. |
s |
String array |
Example 1:
Matlab
A = 57*ones(1,5);
txt = sprintf( '%d %f %o %X %u' , A)
|
Output:
txt = 57 57.00000 71 39 57
Subtype: Before the conversion character, the subtype is the field is represented by a single alphabetic character. It mainly has two types:
S.No. |
Subtype specifier |
Description |
1. |
t |
The input data are single-precision floating-point values rather than unsigned integers. |
2. |
b |
The input data are double-precision floating-point values rather than unsigned integers. |
Example 2:
Matlab
subtype_ = 19;
final_sub = sprintf( '%tu' ,subtype_)
|
Output:
final_sub = 1100480512
Precision: The next field of formatting operators is a precision field. It is usually denoted with dot notation, it is a nonnegative integer that immediately follows a period.
Example 3:
Matlab
check_decimal = sprintf( '%g %.2g %f %.2f' , [1 2 3 4])
|
Output:
check_decimal = 1 2 3.000000 4.00
We took two g and 2 f format specifiers and can see what a Precision does to the decimal point.
Field Width: A Field Width is a non-negative integer that specifies the number of digits or characters in the output. Specify different field widths. To show the width for each output, use the | character. For example, in the operator %5.1f, the field width is 5 and 0.1 precision.
Example 4:
Matlab
text = sprintf( '|%e|%6e|%f|%12f|' ,[0.455312 12345 2223 0.111123])
|
Output:
text = '|4.553120e-01|1.2345e+04|2223000000| 0.111123|'
Flags: The flags field is optional. If used then it mainly controls the formatting of the output. Additionally, it is used to describe padding, text alignment, and spacing.
S.No. |
Char |
Meaning |
Example |
1. |
– |
Left-justify the output text |
%-7.4d |
2. |
+ |
Right-justify the output text |
%+7.4d |
3. |
0 |
Zero Padding |
%06.3f |
4. |
# |
Numeric conversions:
- For %o, %x, or %X, print 0, 0x, or 0X prefix.
- For %f, %e, or %E, a print decimal point even when precision is 0.
- For %g or %G, do not remove trailing zeroes or decimal points.
|
%#4.0f |
5. |
Space |
Insert a space before the value. |
% 4.2f |
Identifier: Identifiers are declared after the % and before the $ special characters. It is an integer value to produce the output in a specified order. sprintf by default prints in sequential order, using Identifier we can arrange output in the custom order.
Example 5:
Matlab
sprintf( '%3$s %2$s %1$s' ,...
'Geeks' , 'Premier' , 'League' )
|
Output:
ans = 'League Premier Geeks'
Formatting Text With compose():
The compose function is used to format the array data into multiple strings.
Syntax:
var_name = compose(format_specifier,var_name)
str = compose(format_specifier,A,b,...,N)
Example:
Matlab
compose_str1 = compose( "%.5f" ,45.566)
compose_str2 = compose( "%.1f" ,45.566)
|
Output:
"compose_str1 = 45.56600"
"compose_str1 = 45.6"
Formatting Text With num2str():
The num2str function is used to format the number into a character array.
Syntax:
new_var = num2str(int_var)
new_var = num2str(int_var,precision)
new_var = num2str(int_var,format_specifier)
Example:
Matlab
num = 92;
chr = num2str(num)
|
Output:
chr = '92'
Similar Reads
Nested Functions in MATLAB
Functions in any programming language are some blocks of code, which could be reused whenever required, by just calling the name. It reduces so much of human effort and also rewriting of the same code, and makes the entire code big. Declaring a Function: To declare a function in MATLAB we use given
3 min read
Curve Fitting in MATLAB
Curve fitting is the method of finding a suitable equation for a given data or one could say finding a pattern in some random data. The goal of this article is to learn curve fitting using MATLAB thus, it is expected that the reader has knowledge of curve fitting in mathematical terms. Step of Curve
2 min read
Importing Data in MATLAB
MATLAB provides a simple way of importing data into a script using the importdata() function. This function takes various inputs and can be used in following forms. Method 1:This simply takes the data file and creates a suitable struct for the same within the script. Example 1: [GFGTABS] Matlab % M
2 min read
Column Vectors in MATLAB
Column vectors are vectors that have a single column but, multiple rows. MATLAB provides various functions that use column vectors as their arguments. In this article, we will see different methods of creating and using column vectors in MATLAB. Creating Column Vectors:Method 1:The simplest way of c
2 min read
Factorial in MATLAB
MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. In this article, we'll be calculating the factorial of a number n using MATLAB's built-in function 'factorial(number)'. Factorial:The
1 min read
Getting Started with MATLAB
MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. It was developed by Cleve Molar of the company MathWorks.Inc in the year 1984. It is written in C, C++, Java. It allows matrix manipulations, plotting of functions, implementation of algorith
3 min read
Data Type Conversion in MATLAB
Data Types in MATLAB is the upheld information organizes that are utilized for calculation. MATLAB is a well-known numerical and factual information investigation instrument that has a large number of elements for calculation. The different kinds of data type MATLAB supports are numeric types, chara
3 min read
Fourier transform in MATLAB
Fourier Transform is a mathematical technique that helps to transform Time Domain function x(t) to Frequency Domain function X(Ï). In this article, we will see how to find Fourier Transform in MATLAB. The mathematical expression for Fourier transform is:[Tex]X(Ï) = F\{x(t)\} = â«_{-â}^âx(t).e^{(-jÏt)
3 min read
Write Data to Text Files in MATLAB
Writing data to a text file means creating a file with data that will be saved on a computer's secondary memory such as a hard disk, CD-ROM, network drive, etc. fprintf() function is used to write data to a text file in MATLAB. It writes formatted text to a file exactly as specified. The different e
3 min read
How to create a function in MATLAB ?
A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: fun
2 min read