Using Strings in MATLAB
Strings in MATLAB are used to represent text. They are essential for handling textual data in various applications. MATLAB provides numerous commands for manipulating strings effectively.
Basic Concepts
Strings in MATLAB are enclosed in double quotes (""
). You can create strings using the following syntax:
% Creating a string
str = "Hello, MATLAB!";
disp(str);
String Array in MATLAB are easily formed using double quotes (""
). You can create strings array using the following syntax:
% Creating a string array
str = ["A","B","C";
"Apple","Bird","Cat"]; % It will form 2 x 3 string array.
disp(str);
Two convert numerical data into the string in Matlab you can use string(N)
or num2str(N)
commands. You can create strings fron mumbers using the following syntax:
% Converting Numerical data in strings using Matlab
X = 21;
Y = 23;
N = [1 2 3 4];
I = [5 6 7 8];
str1 = string(X);
str2 = num2str(Y);
str3 = string(N);
str4 = num2str(I);
disp(str1); % Output: '21'
disp(str2); % Output: '23'
disp(str3); % Output: "1' "2' "3' "4'
disp(str4); % Output: "5" "6" "7" "8"
Common String Operations
Here are some common operations you can perform with strings:
Concatenation
Joining two strings using + operator:
% Concatenating strings
str1 = "Hello";
str2 = "World";
result = str1 + " " + str2;
disp(result);
% Output Displays: Hello World
Explanation: The +
operator combines two strings. In this case, str1
and str2
are concatenated with a space in between.
Joining two strings using append:
% Concatenating strings
str1 = "Hello";
str2 = "World";
result = append(str1,' ',str2)
disp(result);
% Output Displays: Hello World
Explanation: The append
is used for merging different input strings. Note that, for adding a space between the input strings, we have specified a space character as another input argument. In this case, str1
and str2
are concatenated with a space in between. Similar results can also be acheived by using square brackets, for example: result = [str1 " " str2]
Joining two strings array in Matlab using append:
% Concatenating two string array
FirstName = ["Albert " "Charles " "Nikola "];
LastName = ["Einstein" "Darwin" "Tesla"];
Result = append(FirstName,LastName);
disp(Result);
% Output Displays: 1x3 string
% Output: "Albert Einstein" "Charles Darwin" "Nikola Tesla"
Joining numerical data with strings in Matlab:
% Joining a string with numerical value in Matlab
str1 = "Total number of the Students: " + 50 ;
str2 = ['Total number of the Students: ' num2str(40)];
disp(str1); % Output: Total number of the Students: 50
disp(str2); % Output: Total number of the Students: 40
Explanation: In Matlab, you can also join numerical data with string or string arrays using string
or num2str
commands. In this example different types of dataset (string and numbers) are joined as shown above.
Splitting Strings in Matlab
Breaking strings at delimiter or white space in Matlab:
% Splitting text strings
str1 = "God bless America";
str2 = "USA,India,Japan,France";
A = split(str1); % It will split the string at white spaces
B = split(str1, ","); % It will split the string at ','
disp(A); % Output: 3x1 string
disp(B); % Output: 3x1 string
Finding String Length
% Finding the length of a string
str = "MATLAB";
len = strlength(str);
disp(len);
% Output Displays: 6
Explanation: The strlength
function calculates the number of characters in the string.
Extracting Substrings
% Extracting a substring
str = "Hello, MATLAB!";
substr = extractBetween(str, 1, 5);
disp(substr);
% Output Displays: Hello
Explanation: The extractBetween
function retrieves a substring by specifying the starting and ending indices.
Displaying Strings
MATLAB provides several functions to display strings, such as:
disp
The disp
function displays text or variables without formatting:
% Displaying text
disp("Welcome to MATLAB!");
fprintf
The fprintf
function allows formatted output with placeholders:
% Formatted display
fprintf("The value of pi is %.2f\n", pi);
Explanation: The %.2f
specifies the format (two decimal places for a floating-point number).
sprintf
The sprintf
function works like fprintf
but returns a formatted string instead of displaying it:
% Creating a formatted string
str = sprintf("Pi rounded to two decimals: %.2f", pi);
disp(str);
List of useful string functions in Matlab
str = "Academic Block"
creates a text string from the text enclosed in double quotes. str = string(N);
creates the string array from the input array N. str = num2str(A)
creates a text string (or array) from the number (or numeric array) represented by A.str = append(str1,str2, ...., strN)
join all the text strings (or text arrays) enclosed in the brackets (str1,str2, …., strN).str = split(str1)
creates a text string array by breaking the text string enclosed in brackets, at whitespace characters. str = split(str1, delimiter)
creates the string array from the input string str1, by breaking str1 at specified delimiter. Num = strlength(str)
counts the total number of characters in input text string (or array). If the input is a string array, then it will provide the number of characters for each string array element.SubStr = extractBetween(str,str1,str2)
extracts the substring text from the input str that lies between the str1 and str2.N = strfind(str,str1)
returns a array of starting index of each occurrence of ‘str1’ in ‘str’. If ‘str1’ is not found in ‘str’, then strfind will return a empty array, [ ]. Function strfind performs a case-sensitive search.NewString = replace(str,old,new)
replaces all the occurrences of the ‘old’ substring with ‘new’ substring. Practice Questions
Test Yourself
1. Create a string “MATLAB Programming” and extract the word “Programming”.
2. Format a string to display the square of a number using sprintf
.
3. Concatenate “Hello” and “World” with a comma and space in between.