MATLAB Characters & Strings

MATLAB Tutorial

🔤 MATLAB Characters & Strings

Characters & Strings in MATLAB are used to store and manipulate text data such as names, messages, and labels.

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

A

🔸 Example 2: Character Array (Text)


Output

MATLAB

🔸 Example 3: Character Array Size


Output

1 5

📌 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

Delhi

🔸 Example 5: String Length


Output

5

🔁 Difference Between char and string

Featurecharstring
Quotes' '" "
Data typeCharacter arrayString object
Text handlingBasicAdvanced
RecommendedOlder MATLABModern MATLAB

🔗 String Concatenation

🔸 Using char


Output

Hello World

🔸 Using string


Output

Hello World

✂️ String Functions (Commonly Used)

🔹 Length


Output

6

🔹 Convert char ↔ string



🔹 Compare Strings


Output

1

🔹 Upper & Lower Case


Output

MATLAB
matlab

🎯 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

  • " " → string

  • string is easier & modern

  • Many built-in string functions available

You may also like...