Opening the book…
|
Mistake |
Why it happens |
How to fix it |
|
Confusing "text" and 'text' |
Double quotes create strings, single quotes create character vectors. |
Check the type with isstring or ischar. |
|
Using length when you mean character count |
length treats strings as array elements. |
Use strlength to count characters. |
|
Forgetting that text comparisons are case sensitive |
"cat" and "Cat" are different. |
Use strcmpi or convert both values with lower. |
|
Forgetting spaces count as characters |
Spaces are real characters in text. |
Count positions carefully or use split/strtrim. |
|
Using str2double but getting NaN |
The text does not represent a valid number. |
Check the input and remove extra nonnumeric characters. |
|
Not trimming user input |
Users may type accidental spaces. |
Use strtrim before comparing or converting. |
This page titled 7.13: Common Mistakes and Best Practices was last modified on Tue, 30 Jun 2026 02:11:54 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by .
The book could not be loaded just now.