MATLAB Answers

0

Help with splitting a string into a character array.

Asked by Benjamin Carpenter on 5 Nov 2017
Latest activity Commented on by Benjamin Carpenter on 5 Nov 2017
Let's say I have a string s='Potato'.
How would I split that up into an array so that it looks like t='P, o, t, a, t, o'?
I'm currently doing it like this:
%%Choose word
word=datasample(wordbank,1)
%identify length of word
wordlength=strlength(word)
sol = split(word,"")
The only problem with doing it this way is that there are two blank spots and the beginning and end of the character array. It looks like this when outputted:
"" "P" "o" "t" "a" "t" "o" ""
How can I split it so that the blank values at the beginning and end aren't there?

  0 Comments

Sign in to comment.

1 Answer

Answer by Mischa Kim
on 5 Nov 2017
Edited by Mischa Kim
on 5 Nov 2017
 Accepted Answer

You could use
s = 'Potato';
t = num2cell(s)
t =
1×6 cell array
{'P'} {'o'} {'t'} {'a'} {'t'} {'o'}

  1 Comment

Sign in to comment.