MATLAB Characters & Strings

🔤 MATLAB Characters & Strings
MATLAB is developed by MathWorks.
🔹 What Are Characters & Strings in MATLAB?
Character array (char) → Uses single quotes
' 'String data type (string) → Uses double quotes
" "
1️⃣ Character Data Type (char)
A character array stores text as an array of characters.
🔸 Example 1: Single Character
Output
🔸 Example 2: Character Array (Text)
Output
🔸 Example 3: Character Array Size
Output
📌 Character strings are treated as arrays of characters.
2️⃣ String Data Type (string)
Strings were introduced to provide easier text handling.
🔸 Example 4: String Variable
Output
🔸 Example 5: String Length
Output
🔁 Difference Between char and string
| Feature | char | string |
|---|---|---|
| Quotes | ' ' | " " |
| Data type | Character array | String object |
| Text handling | Basic | Advanced |
| Recommended | Older MATLAB | Modern MATLAB |
🔗 String Concatenation
🔸 Using char
Output
🔸 Using string
Output
✂️ String Functions (Commonly Used)
🔹 Length
Output
🔹 Convert char ↔ string
🔹 Compare Strings
Output
🔹 Upper & Lower Case
Output
🎯 Interview Questions: Characters & Strings
🔹 Q1. What is the difference between char and string in MATLAB?
Answer:char is a character array using single quotes, while string is a modern data type using double quotes with advanced features.
🔹 Q2. Which is recommended: char or string?
Answer:string is recommended for new MATLAB programs.
🔹 Q3. How do you find the length of a string?
Answer:
Using strlength().
🔹 Q4. How do you concatenate strings?
Answer:
Using + for strings and [ ] for char arrays.
🔹 Q5. How do you compare two strings?
Answer:
Using strcmp().
🔹 Q6. Can a character array store multiple words?
Answer:
Yes, but all rows must have equal length.
🔹 Q7. How do you convert char to string?
Answer:
Using string() function.
✅ Summary
' '→ char" "→ stringstringis easier & modernMany built-in string functions available
