Strings and character arrays both store text in MATLAB, but they work differently. Knowing the difference helps you choose the right one for your task.
String vs character array in MATLAB
str = "Hello"; % String charArray = ['H', 'e', 'l', 'l', 'o']; % Character array
Strings are created with double quotes " ".
Character arrays use single quotes ' ' around each character or a single quoted sequence.
str = "Hello, world!"; disp(str);
charArray = ['H', 'e', 'l', 'l', 'o']; disp(charArray);
emptyStr = ""; emptyCharArray = ''; disp(emptyStr); disp(emptyCharArray);
singleCharStr = "A"; singleCharArray = 'A'; disp(singleCharStr); disp(singleCharArray);
This program shows how to create a string and a character array, access their first characters, and modify the second character in both.
str = "Hello"; charArray = ['H', 'e', 'l', 'l', 'o']; fprintf('String: %s\n', str); fprintf('Character array: %s\n', charArray); % Access first character fprintf('First character of string: %s\n', str(1)); fprintf('First character of char array: %s\n', charArray(1)); % Modify second character str(2) = 'a'; charArray(2) = 'a'; fprintf('Modified string: %s\n', str); fprintf('Modified char array: %s\n', charArray);
Strings are easier to work with for most text operations because MATLAB has many built-in string functions.
Character arrays are older and sometimes needed for compatibility with legacy code.
Strings use more memory but offer more features like string arrays and text processing.
Strings use double quotes and are modern, flexible text containers.
Character arrays use single quotes and treat text as a list of characters.
Choose strings for new code and character arrays for legacy or specific character-level tasks.
Practice
Solution
Step 1: Understand string syntax in MATLAB
Strings in MATLAB are enclosed in single quotes, like 'Hello World'.Step 2: Identify string and character array syntax
In MATLAB, single quotes denote character arrays (which are often used as strings). Double quotes denote string arrays introduced in newer MATLAB versions, but the classic string representation is single quotes.Final Answer:
'Hello World' -> Option AQuick Check:
Single quotes denote character arrays (strings) = B [OK]
- Confusing single quotes for strings
- Using curly braces which create cell arrays
- Using parentheses which are invalid for strings
Solution
Step 1: Recognize character array syntax
Character arrays are created using single quotes around text, like 'abc'.Step 2: Differentiate from string and cell array
Double quotes create string arrays, and curly braces create cell arrays, so only single quotes create character arrays.Final Answer:
charArray = 'abc'; -> Option DQuick Check:
Character arrays use single quotes = C [OK]
- Using double quotes for character arrays
- Using curly braces which create cell arrays
- Using square brackets with commas incorrectly
str = "MATLAB"; charArr = 'MATLAB'; disp(class(str)); disp(class(charArr));
Solution
Step 1: Identify variable types
Variablestris created with double quotes, so it is a string type. VariablecharArruses single quotes, so it is a character array (char).Step 2: Understand
Theclass()outputclass()function returns the type name as a string. Soclass(str)returns 'string' andclass(charArr)returns 'char'.Final Answer:
string char -> Option AQuick Check:
Double quotes = string, single quotes = char [OK]
- Assuming both are 'char' type
- Confusing output order
- Thinking class() returns variable content
str = 'Hello'; str(1) = "h";
What is the error in this code?
Solution
Step 1: Identify variable type
Variablestris created with single quotes, so it is a character array.Step 2: Understand assignment rules for character arrays
Character array elements must be assigned single characters using single quotes. Assigning a double-quoted string "h" causes a type mismatch error.Final Answer:
Cannot assign double quotes to a character array element -> Option BQuick Check:
Character arrays need single quotes for elements [OK]
- Using double quotes to assign char array elements
- Thinking MATLAB indexing starts at 0
- Assuming strings can't be indexed
Solution
Step 1: Understand types of inputs
"Data" is a string. 'Science' is a character array. To concatenate strings, both must be string type.Step 2: Convert character array to string before concatenation
Usingstring('Science')converts the char array to string, allowing + operator to concatenate strings properly.Final Answer:
result = "Data" + string('Science'); -> Option CQuick Check:
Convert char to string before + concatenation [OK]
- Trying to add string and char directly
- Using + with char arrays (causes error)
- Not converting char to string before concatenation
Ready to prove it?
Pass the test and this pattern moves unknown
