How can I put single quotes inside single quotes?

21 views (last 30 days)
vector = ['a', 'b', 'c','d', '1', '2', '3', '!', '@', ' " ', ''' ];
in the above code segment the vector contain different elements but the last element which is single quote is not placed properly. so, how can i put it correctly?
  1 Comment
Stephen23
Stephen23 on 8 Dec 2022
Square brackets are a concatenation operator, so this code:
V1 = ['a', 'b', 'c', 'd', '1', '2', '3', '!', '@', '"', '''' ]
is exactly equivalent to (but a pointlessly long and complex way of) simply writing this:
V2 = 'abcd123!@"'''
How to escape single quotes in character vectors is explained in the MATLAB documentation:
"If the text includes single quotes, use two single quotes within the definition."

Sign in to comment.

Accepted Answer

DGM
DGM on 8 Dec 2022
You can escape single quotes with another single quote. For sake of clarity, I'm going to transpose the output.
vector = ['a', 'b', 'c','d', '1', '2', '3', '!', '@', '"', '''' ].'
vector = 11×1 char array
'a' 'b' 'c' 'd' '1' '2' '3' '!' '@' '"' '''

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!