Using Strings in MATLAB Get Certified
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 arrays in MATLAB are easily formed using double quotes. You can create a string array using the following syntax:
% Creating a string array
str = ["A","B","C";
"Apple","Bird","Cat"]; % 2 x 3 string array
disp(str);
To convert numerical data into strings you can use string(N) or num2str(N):
% Converting numerical data into strings
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); % '21'
disp(str2); % '23'
disp(str3); % "1" "2" "3" "4"
disp(str4); % 5 6 7 8
Common String Operations
Here are some common operations you can perform with strings:
Concatenation
Joining two strings using the + operator:
% Concatenating strings with +
str1 = "Hello";
str2 = "World";
result = str1 + " " + str2;
disp(result);
Joining two strings using append:
% Concatenating strings with append
str1 = "Hello";
str2 = "World";
result = append(str1, " ", str2);
disp(result);
Joining two string arrays with append:
% Concatenating two string arrays
FirstName = ["Albert " "Charles " "Nikola "];
LastName = ["Einstein" "Darwin" "Tesla"];
Result = append(FirstName, LastName);
disp(Result);
Joining numerical data with strings:
% Joining a string with a numerical value
str1 = "Total number of the Students: " + 50;
str2 = ['Total number of the Students: ' num2str(40)];
disp(str1);
disp(str2);
Splitting Strings
Breaking strings at whitespace or a delimiter:
% Splitting text strings
str1 = "God bless America";
str2 = "USA,India,Japan,France";
A = split(str1); % split at whitespace
B = split(str2, ","); % split at commas
disp(A);
disp(B);
Finding String Length
% Finding the length of a string
str = "MATLAB";
len = strlength(str);
disp(len);
Extracting Substrings
% Extracting a substring
str = "Hello, MATLAB!";
substr = extractBetween(str, 1, 5);
disp(substr);
Displaying Strings
MATLAB provides several functions to display strings:
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);
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. str = string(N) creates a string array from the numeric array N.str = num2str(A) creates a character array from the number (or numeric array) A.str = append(str1,str2,...,strN) joins the supplied strings or string arrays.str = split(str1) splits at whitespace. str = split(str1, delimiter) splits at the given delimiter.Num = strlength(str) returns the number of characters in each element of the string (or string array).SubStr = extractBetween(str, start, end) extracts the characters between the given positions.N = strfind(str, pattern) returns the starting indices of every occurrence of the pattern (case-sensitive).NewString = replace(str, old, new) replaces every occurrence of the old substring with the new one.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.