| id | matlab-string | ||
|---|---|---|---|
| title | String | ||
| sidebar_label | MATLAB String | ||
| sidebar_position | 6 | ||
| tags |
|
||
| description | In this tutorial, you will learn about String in MATLAB. Strings are arrays of characters enclosed in single quotes (`'`). Starting from MATLAB R2016b, strings are also supported as a data type (`string`). |
In MATLAB, strings are arrays of characters enclosed in single quotes ('). Starting from MATLAB R2016b, strings are also supported as a data type (string).
% Creating strings
str1 = 'Hello, ';
str2 = 'MATLAB!';
% Concatenating strings
fullStr = [str1 str2];
% Displaying the concatenated string
disp(fullStr);Output:
Hello, MATLAB!
% Extracting a substring
sentence = 'The quick brown fox jumps over the lazy dog';
substr = extractBetween(sentence, 5, 9);
% Converting to uppercase
upperSentence = upper(sentence);
% Finding the position of a substring
pos = strfind(sentence, 'brown');
% Displaying results
disp(substr);
disp(upperSentence);
disp(pos);Output:
quick
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
10
MATLAB provides a variety of functions for working with strings. Here are a few commonly used ones:
strcat: Concatenates strings.strcmp: Compares strings.strsplit: Splits strings based on delimiter.sprintforfprintf: Formats strings like in C programming.
% Formatting strings
name = 'Alice';
age = 30;
height = 1.75;
% Creating a formatted string
str = sprintf('Name: %s, Age: %d, Height: %.2f meters', name, age, height);
% Displaying formatted string
disp(str);Output:
Name: Alice, Age: 30, Height: 1.75 meters
Starting from MATLAB R2016b, strings can be stored in string arrays, which are more flexible for handling multiple strings.
% Creating a string array
names = ["Alice", "Bob", "Charlie", "David"];
% Accessing elements
disp(names(2)); % Accessing the second element
% Concatenating string arrays
fullNames = strcat(names, " Johnson");
% Displaying results
disp(fullNames);Output:
Bob
"Alice Johnson"
"Bob Johnson"
"Charlie Johnson"
"David Johnson"
MATLAB provides robust support for working with strings, whether as character arrays or as string objects. Understanding these fundamentals allows for effective manipulation and analysis of textual data within MATLAB programs.