Split uint8 vector if there is a Space (' ') (32)

1 view (last 30 days)
I have for example a vector:
72 101 108 108 111 32 73 32 97 109 32 51 48 32 121 101 97 114 115 32 111 108 100 46
which means,
"Hello I am 30 years old.".
Now I want to split the vector after each space (' ') in a form like this:
72 101 108 108 111
73 32 97 109
51 48 32 121 101 97 114 115
111 108 100 46
and write this into an array or a new vetor to see if it is equal with, for example (72 101 108 108 111).

Answers (1)

Guillaume
Guillaume on 28 Apr 2015
I'm not sure why you're working with uint8 which restricts pretty much to ASCII (I don't think matlab support code pages), when using actual char would make your life easier. Furthermore char allows you to use unicode instead of ASCII.
For what you want, I'd just convert to char and use strsplit:
v = [72 101 108 108 111 32 73 32 97 109 32 51 48 32 121 101 97 114 115 32 111 108 100 46];
w = strsplit(char(v))
To get back to uint8:
w = cellfun(@(s) uint8(s), w, 'UniformOutput', false)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!