Updated March 13, 2023
Introduction to Matlab string
Matlab string is used to represent data in a sequence of characters. It represents the data like text using string arrays instead of character format. It stores every element into a sequence format. In it, we can represent data like text, numbers, special symbols, date time, duration, or calendar duration array into a string array. Its function converts the data type from the input array to string array format. And if data is already into a string array, which represented numbers, then by using the double function we can convert it to a numeric array.
Syntax:
str = string(A)
str = string(D)
How string Data Type works in Matlab?
It is used for representing the input data into a sequence format. There are simple following steps for use of the Matlab string function.
- Step 1: Load the data which can be of any format.
- Step 2: Use the appropriate syntax.
- Step 3: Execute the Matlab code for getting output.
Examples of Matlab string
Given below are the examples mentioned:
Example #1
In this example, we can convert a text array into a string array using the Matlab string function and then we measure the size of that array. It is used for representing the input data into a sequence format. So first you take data which can be in different formats like text, numbers, special symbols, date time, duration, or calendar duration array and that data can be represented in string format using function. In this example, we take data which is into the text array format. Then we create an array that includes the words in text format. “A = [‘ Have ‘, ‘ A ‘, ‘ nice ‘, ‘ day’ ];” this line creates a text array of the word. So there are a total number of words is 4 into a text data into an array. As we know that to convert the text array into the string array we use the function. Then we use a function with proper syntax to convert the input array to a string array. “str = string(A)” this syntax is used for converting the input array to a string array.
Code:
clc;
clear all;
close all;
A = ['Have',' A',' nice',' day'];
str = string(A)
st = size(A)
Output:
After executing the Matlab code the input array is converted into a string array and generated array.
Example #2
In this example, we can convert the duration array into a string array using Matlab string function. As we know that it is used for representing the input data into a sequence or string format. So first you take data so here data is in the duration format. For that, we create a duration array. “D = hours (20:25) + minutes (28) + seconds (2.2345);” this line is used to get the data into the duration array. The data is in duration array which includes hours, minutes, and seconds type of data. As we know that to convert the duration array into the string array we use the function. Then we use a function with proper syntax to convert the duration array to a string array. “str = string(D)” this syntax is used for converting the input array to a string array, where D is duration array. “str” is created string array.
Code:
clc;
clear all;
close all;
D = hours(20:25) + minutes(28) + seconds(2.2345);
str = string(D)
Output:
After executing the Matlab code string array is created into the command window.
Example #3
In this example, we can convert the character vector into a string array using the Matlab string function. So first you take data. So here data is in the format of a character vector. “A = [‘MATLAB is a multi-numerical environment.’];” this line assign character vector to variable A. Then we use the function with proper syntax to convert the duration array to a string array. “str = string(A)” this syntax is used for converting the Character Vector to a string array, where A is Character Vector and “str” is created string array.
Code:
clc;
clear all;
close all;
A = ['MATLAB is a multi-numerical environment.'];
str = string(A)
c = size(A)
Output:
Conclusion
In this article, we saw the concept of Matlab string. Basically, it is used to convert the data type from the input array to string array format. Then we saw syntax related to string statements and how it is used in Matlab. It plays a very important role while converting data into a string array.
Recommended Articles
This is a guide to Matlab string. Here we discuss the introduction, how string data type work in Matlab? and examples respectively. You may also have a look at the following articles to learn more –