What is the difference between a character array and a string in MATLAB?
A character array is a sequence of characters stored as a 2D array of type char. A string is a MATLAB string object created with double quotes, which offers more functions and easier manipulation.
Click to reveal answer
beginner
How do you create a character array and a string in MATLAB?
Use the + operator for strings: str1 + str2. For character arrays, use [charArray1, charArray2].
Click to reveal answer
intermediate
What function converts a string to a character array in MATLAB?
Use the char() function. For example, charArray = char(str); converts string str to a character array.
Click to reveal answer
beginner
How do you find the length of a string or character array in MATLAB?
Use strlength() for strings and length() or size() for character arrays. Example: strlength(str) or length(charArray).
Click to reveal answer
Which of the following creates a string in MATLAB?
Astr = "Hello";
Bstr = ['H', 'e', 'l', 'l', 'o'];
Cstr = {"Hello"};
Dstr = ('Hello');
✗ Incorrect
Strings in MATLAB are created using double quotes, like "Hello".
How do you concatenate two character arrays c1 and c2?
A[c1, c2]
Bstrcat(c1, c2)
Cc1 + c2
Dappend(c1, c2)
✗ Incorrect
Concatenate character arrays by placing them inside square brackets: [c1, c2].
What does strlength("MATLAB") return?
AError
B6
C0
D1
✗ Incorrect
strlength returns the number of characters in the string, which is 6 for "MATLAB".
Which function converts a string to a character array?
Aconvert()
Bstring()
Cchar()
Darray()
✗ Incorrect
The char() function converts a string to a character array.
What is the output of class('Hello') in MATLAB?
A"string"
B"double"
C"cell"
D"char"
✗ Incorrect
Single quotes create a character array of class char.
Explain how to create, concatenate, and find the length of strings and character arrays in MATLAB.
Think about the differences in syntax and functions for strings vs character arrays.
You got /6 concepts.
Describe how to convert between strings and character arrays in MATLAB and why you might want to do that.
Consider the advantages of each type and compatibility with MATLAB functions.
You got /5 concepts.
Practice
(1/5)
1. Which of the following correctly creates a character array in MATLAB?
easy
A. name = [Hello];
B. name = "Hello";
C. name = 'Hello';
D. name = (Hello);
Solution
Step 1: Understand character array syntax
Character arrays in MATLAB use single quotes to enclose text.
Step 2: Check each option
name = 'Hello'; uses single quotes correctly. name = "Hello"; uses double quotes which create string objects, not character arrays. Options A and D are invalid syntax.
Final Answer:
name = 'Hello'; -> Option C
Quick Check:
Single quotes create character arrays [OK]
Hint: Use single quotes for character arrays, double for strings [OK]
Common Mistakes:
Using double quotes for character arrays
Missing quotes around text
Using brackets or parentheses incorrectly
2. Which of the following is the correct way to create a string in MATLAB?
easy
A. str = "MATLAB";
B. str = 'MATLAB';
C. str = {"MATLAB"};
D. str = ("MATLAB");
Solution
Step 1: Recall string creation syntax
Strings in MATLAB are created using double quotes.
Step 2: Evaluate options
str = "MATLAB"; uses double quotes correctly. str = 'MATLAB'; creates a character array, not a string. Options C and D use invalid or different data types.
C. Error: Cannot concatenate char array and string
D. abc def
Solution
Step 1: Understand concatenation of char arrays and strings
In MATLAB, concatenating a character array and a string with [] converts the string to a char array and joins them.
Step 2: Evaluate the concatenation result
Concatenating 'abc' and "def" results in 'abcdef' as a character array, displayed without spaces.
Final Answer:
abcdef -> Option A
Quick Check:
Concatenation joins text without spaces [OK]
Hint: Concatenate char arrays and strings with [] to join text [OK]
Common Mistakes:
Expecting spaces between concatenated strings
Thinking concatenation causes error
Confusing display output with variable type
4. Identify the error in the following MATLAB code:
str1 = 'Hello';
str2 = "World";
result = strcat(str1, str2);
medium
A. strcat cannot concatenate character arrays and strings
B. No error, code runs correctly
C. Missing semicolon after str2 assignment
D. str1 and str2 must both be strings or both char arrays
Solution
Step 1: Understand strcat behavior
strcat can concatenate character arrays and strings by automatically converting strings to character arrays.
Step 2: Verify the code
str1 is a character array, str2 is a string, but strcat(str1, str2) returns 'HelloWorld' as a character array. No error occurs.
Final Answer:
No error, code runs correctly -> Option B
Quick Check:
strcat handles mixed types [OK]
Hint: strcat automatically handles char and string inputs [OK]
Common Mistakes:
Assuming strcat requires matching types
Confusing with + operator which doesn't work for mixed types
Looking for syntax errors where none exist
5. You have a character array chars = 'MATLAB' and want to convert it to a string and append " is fun". Which code correctly does this?
hard
A. newStr = chars + " is fun";
B. newStr = chars + ' is fun';
C. newStr = strcat(chars, " is fun");
D. newStr = string(chars) + " is fun";
Solution
Step 1: Convert character array to string
Use string(chars) to convert the character array to a string type.
Step 2: Append text using + operator
Strings can be concatenated with + and another string, so adding " is fun" works.
Step 3: Check other options
newStr = chars + ' is fun'; tries to add char array and char array with + which is invalid. newStr = strcat(chars, " is fun"); mixes types but produces a character array, not a string. newStr = chars + " is fun"; adds char array and string with + which errors.
Final Answer:
newStr = string(chars) + " is fun"; -> Option D
Quick Check:
Convert char to string, then use + to join [OK]
Hint: Convert char array to string first, then use + to append [OK]
Common Mistakes:
Using + to add char arrays
Mixing char arrays and strings in strcat
Not converting char array before concatenation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.