Bird
Raised Fist0
MATLABdata~10 mins

String vs character array in MATLAB - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - String vs character array
Create string variable
↓
Create character array variable
↓
Access elements
↓
Modify elements
↓
Compare behavior
↓
Understand differences
This flow shows how MATLAB handles strings and character arrays differently, from creation to element access and modification.
Execution Sample
MATLAB
str = "Hello";
charArr = ['H','e','l','l','o'];
str(1) = "J";
charArr(1) = 'J';
disp(str);
disp(charArr);
This code creates a string and a character array, modifies their first elements, and displays the results.
Execution Table
StepVariableOperationValue BeforeValue AfterOutput
1strCreate stringN/A"Hello"N/A
2charArrCreate char arrayN/A['H','e','l','l','o']N/A
3strModify first char"Hello""Jello"N/A
4charArrModify first char['H','e','l','l','o']['J','e','l','l','o']N/A
5strDisplay"Jello""Jello"Jello
6charArrDisplay['J','e','l','l','o']['J','e','l','l','o']Jello
💡 All operations complete; string and char array modified and displayed.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
strN/A"Hello""Jello""Jello"
charArrN/A['H','e','l','l','o']['J','e','l','l','o']['J','e','l','l','o']
Key Moments - 2 Insights
Why can we assign a string "J" to str(1) but must use a character 'J' for charArr(1)?
In MATLAB, strings are arrays of string elements, so str(1) expects a string type ("J"), while character arrays are arrays of characters, so charArr(1) expects a single character ('J'). This is shown in steps 3 and 4 of the execution_table.
Why does displaying charArr show as Jello instead of ['J','e','l','l','o']?
When displaying a character array, MATLAB prints it as a word by concatenating characters, so ['J','e','l','l','o'] appears as Jello. This is shown in step 6 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of str after step 3?
A"Jello"
B"Hello"
C['J','e','l','l','o']
D['H','e','l','l','o']
💡 Hint
Check the 'Value After' column for str at step 3 in the execution_table.
At which step does charArr first change its value?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Operation' and 'Value After' columns for charArr in the execution_table.
If we tried to assign 'J' (single quotes) to str(1), what would happen?
AIt would work and change str to 'Jello'
BIt would cause an error because str expects string type elements
CIt would convert 'J' to "J" automatically
DIt would change charArr instead
💡 Hint
Recall the difference in data types for str and charArr shown in steps 3 and 4.
Concept Snapshot
String vs Character Array in MATLAB:
- Strings use double quotes "" and are arrays of string elements.
- Character arrays use single quotes '' and are arrays of characters.
- Modify strings with string elements ("J"), char arrays with characters ('J').
- Displaying char arrays shows concatenated characters as words.
- Strings and char arrays behave differently in indexing and assignment.
Full Transcript
In MATLAB, strings and character arrays are different types. Strings are created with double quotes and hold string elements, while character arrays use single quotes and hold characters. When you modify the first element of a string, you assign a string element like "J". For character arrays, you assign a character like 'J'. Displaying a character array shows the characters combined as a word. This example code creates both types, modifies their first elements, and displays them, showing how MATLAB treats them differently.

Practice

(1/5)
1. Which of the following correctly represents a string in MATLAB?
easy
A. 'Hello World'
B. ('Hello World')
C. {'Hello World'}
D. "Hello World"

Solution

  1. Step 1: Understand string syntax in MATLAB

    Strings in MATLAB are enclosed in single quotes, like 'Hello World'.
  2. 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.
  3. Final Answer:

    'Hello World' -> Option A
  4. Quick Check:

    Single quotes denote character arrays (strings) = B [OK]
Hint: Strings use single quotes in MATLAB [OK]
Common Mistakes:
  • Confusing single quotes for strings
  • Using curly braces which create cell arrays
  • Using parentheses which are invalid for strings
2. Which of the following is the correct way to create a character array in MATLAB?
easy
A. charArray = "abc";
B. charArray = ["a", "b", "c"];
C. charArray = { 'a', 'b', 'c' };
D. charArray = 'abc';

Solution

  1. Step 1: Recognize character array syntax

    Character arrays are created using single quotes around text, like 'abc'.
  2. 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.
  3. Final Answer:

    charArray = 'abc'; -> Option D
  4. Quick Check:

    Character arrays use single quotes = C [OK]
Hint: Use single quotes for character arrays, double for strings [OK]
Common Mistakes:
  • Using double quotes for character arrays
  • Using curly braces which create cell arrays
  • Using square brackets with commas incorrectly
3. What is the output of the following MATLAB code?
str = "MATLAB";
charArr = 'MATLAB';
disp(class(str));
disp(class(charArr));
medium
A. string char
B. char string
C. string string
D. char char

Solution

  1. Step 1: Identify variable types

    Variable str is created with double quotes, so it is a string type. Variable charArr uses single quotes, so it is a character array (char).
  2. Step 2: Understand class() output

    The class() function returns the type name as a string. So class(str) returns 'string' and class(charArr) returns 'char'.
  3. Final Answer:

    string char -> Option A
  4. Quick Check:

    Double quotes = string, single quotes = char [OK]
Hint: class() shows 'string' for double quotes, 'char' for single quotes [OK]
Common Mistakes:
  • Assuming both are 'char' type
  • Confusing output order
  • Thinking class() returns variable content
4. Consider this MATLAB code snippet:
str = 'Hello';
str(1) = "h";

What is the error in this code?
medium
A. Indexing starts at 0, so str(1) is invalid
B. Cannot assign double quotes to a character array element
C. Strings cannot be indexed like arrays
D. No error, code runs fine

Solution

  1. Step 1: Identify variable type

    Variable str is created with single quotes, so it is a character array.
  2. 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.
  3. Final Answer:

    Cannot assign double quotes to a character array element -> Option B
  4. Quick Check:

    Character arrays need single quotes for elements [OK]
Hint: Assign single characters with single quotes to char arrays [OK]
Common Mistakes:
  • Using double quotes to assign char array elements
  • Thinking MATLAB indexing starts at 0
  • Assuming strings can't be indexed
5. You want to concatenate the words "Data" and 'Science' in MATLAB to form a string. Which code correctly does this and results in a string type?
hard
A. result = "Data" + 'Science';
B. result = "Data" + "Science";
C. result = "Data" + string('Science');
D. result = "Data" + char('Science');

Solution

  1. Step 1: Understand types of inputs

    "Data" is a string. 'Science' is a character array. To concatenate strings, both must be string type.
  2. Step 2: Convert character array to string before concatenation

    Using string('Science') converts the char array to string, allowing + operator to concatenate strings properly.
  3. Final Answer:

    result = "Data" + string('Science'); -> Option C
  4. Quick Check:

    Convert char to string before + concatenation [OK]
Hint: Convert char arrays to strings before using + for concatenation [OK]
Common Mistakes:
  • Trying to add string and char directly
  • Using + with char arrays (causes error)
  • Not converting char to string before concatenation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard