Introduction to Matlab sprintf
Matlab provides the different types of functions to the user; sprintf() is one of the functions that is provided by Matlab. Basically, sprintf() is a string variable and that is created in Matlab memory that means we can create the string variable by using the sprintf() instead of writing it into a text file. The working of the sprint() is the same as fprintf() but the only difference is that when we use fprintf() is used to write the data into a text file and by using sprint we can format the data string
string = sprintf(formatSpec, Array1,Array2,…….ArrayN)
[string,error_message] = sprintf (formatSpec, Array1,Array2,…….ArrayN)
string = sprintf (text)
There are multiple syntaxes available for sprintf() in Matlab as shown in the above syntax.
In the first syntax, we specify the data into array formats using the formatSpec formatting operator and that helps us to return the result into a string from the text. Here we also used Array1, Array2 for column order and it is formatted by using the sprint(). Notice here if formatSpec is a string then it returns the result into a string else the return result is a character vector.
In the Second syntax, we use error messages when our transaction is unsuccessful otherwise it shows empty error messages.
In the third line, we try to print the literal text such as \n and \t.
How sprint works in Matlab?
Now let’s see how to use sprintf() in Matlab as follows.
Formatting text is the main part of the sprintf() function. We can format the text as per our requirement by using the different input arguments and different data types. In Matlab, we have an option to format the text under control by using the sprintf() function as well we can use the formatting operator with the different conversion characters.
Sprintf uses different input arguments as follows.
Formatting of output we specified by using different operators. Here we use the formatSpec formatting operator and it includes text and special characters. Sometimes formatSpec uses the literal text at that time sprintf translation of all characters. The format of formatSpec is a single quote or string. Now let’s see the formatting operator in Matlab as follows.
Formatting Operator for sprintf in Matlab
Formatting operator uses % sign to start and with the conversion character compulsory. Instead of conversion characters, we can use the identifier, flag, and width, precision between the % and conversion characters.
Conversion Characters for sprintf in Matlab
Matlab provides the different types of conversion characters such as Integer signed, integer unsigned, floating-point number, and character or string. As per conversion characters we can use different characters such %d or %i for integer signed and when we have a %u that means integer unsigned characters like this we can use different conversion characters as per our requirement.
2 Numeric, character, or string array:
This is another input formatting type of sprintf in Matlab. For string array, character, and Numeric we can use different data types such as single, double, int8, int16, string, logical, etc.
3. Literal Text :
This is one more input text in Matlab but the difference is that; here we can use input text without any formatting operator that means without character vector or string scalar. The literalText uses two types of data types such as char and string.
Now let’s see some output arguments in sprintf as follows.
1. String format:
In this type, we use the formatSpec option to match the output formatted text.
2. Error message:
It is used to return character otherwise it returns the error message when a transaction execution is unsuccessful.
Now let’s see the different examples of the sprintf() function in Matlab for better understanding the sprintf() function as follows.
Now let’s see a simple example of sprintf() as follows.
A = sprintf('In Matlab%s', '!');
B = sprintf('Welcome %s', A);
In the above example, we try to implement how we can join two different strings by using the sprintf() function. In this example, we use two variables A and B and we assign some text to the A and B variable as shown in the above program. But notice here in the second line we try to merge the two words by using the sprintf() function. The final output of this program we illustrated by using the following screenshot as follows.
Now let’s see how we can represent the floating number by using the sprintf() function as follows.
X = 5/8;
A = sprintf('%0.22E',X)
B = sprintf('%5G',X)
C = sprintf('%3f',X)
In the above example, we implemented a floating-point format by using %E, %G, and %f specifiers as shown in the above code. Here %E is used for the exponential value, %G is used in more compact notation such as 5.14 like this, and %f is used to represent fixed-point value. The final output of this program we illustrated by using the following screenshot as follows.
Now let’s see another example of the sprintf() function as follows.
data = sprintf('%f %.2G %f %.2E', pi*30*ones(1,4))
In the above example, we try to implement another example of the sprintf() function. In the above code, we use one function to implement the floating-point number. The final output of this program we illustrated by using the following screenshot as follows.
Now let’s try to implement text as a string array as follows.
formatSpec = "The two integer number: %d+%d %s";
Array1 = 5;
Array2 = 10;
Array3 = "present here with + operator";
A = sprintf(formatSpec,Array1,Array2,Array3)
In the above example, we implemented specified formatted text into a string array as shown in the above program. The final output of this program we illustrated by using the following screenshot as follows.
We hope from this article you learn Matlab sprintf. From the above article, we have learned the basic syntax of sprintf. and we also see different examples of sprintf. From this article, we learned how and when we use Matlab sprintf.
This is a guide to Matlab sprintf. Here we discuss the basic syntax of sprintf. and we also see different examples of sprintf. You may also have a look at the following articles to learn more –