String Functions in MATLAB

MATLAB Tutorial

🔤 String Functions in MATLAB

String functions in MATLAB are used to create, manipulate, search, compare, and format text data efficiently.

Modern MATLAB encourages using string arrays ("text") instead of character arrays ('text').
MATLAB is developed by MathWorks.


🔹 String vs Character Array (Quick)

Type Example Notes
String "Hello" Recommended (rich functions, array-friendly)
Char 'Hello' Legacy; still supported

1️⃣ Creating Strings


Create from char:



2️⃣ Length & Size


Output

6

3️⃣ Concatenation

🔸 + operator (recommended)


Output

"MATLAB R2023a"

🔸 strcat()



4️⃣ Change Case


Output

"MATLAB"
"matlab"

5️⃣ Comparison


Output

1
1

6️⃣ Search & Match

🔸 contains()


Output

1

🔸 startsWith() / endsWith()



7️⃣ Replace Text


Output

"I like MATLAB"

8️⃣ Split & Join

🔸 split()


Output

"red"
"green"
"blue"

🔸 join()


Output

"red-green-blue"

9️⃣ Trim Spaces


Output

"MATLAB"

🔟 Convert Between Types


Output

"123"
45.6

1️⃣1️⃣ Formatting Strings


Output

"Name: Amit | Marks: 85"

1️⃣2️⃣ String Arrays


Output

0 1 1

⚠️ Important Notes

  • Prefer string arrays (" ") over char arrays

  • Many functions are vectorized for string arrays

  • Use sprintf for formatted messages

  • contains, replace, split are exam favorites


🎯 Interview Questions: String Functions in MATLAB

🔹 Q1. Difference between string and char?

Answer:
Strings use double quotes and support richer functions; chars use single quotes.


🔹 Q2. How do you concatenate strings?

Answer:
Using + or strcat().


🔹 Q3. Which function checks substring existence?

Answer:
contains().


🔹 Q4. How do you split a string?

Answer:
Using split().


🔹 Q5. How do you format strings with values?

Answer:
Using sprintf().


🔹 Q6. How do you convert string to number?

Answer:
str2double().


Summary

  • String functions handle text efficiently

  • Use " " (string) instead of ' ' (char)

  • Support searching, replacing, splitting, formatting

  • Essential for file I/O, labels, and reports

You may also like...