Splitting Characters in A Cell Array
Show older comments
Hi All,
I am trying to split some content in a cell array into separate portions. I've tried converting to a string and using strsplit, but I am not getting the results I want because of the datatype syntax.
Came across the cellfun command, but not really sure how to implement it.
Here is what I have
'P245/65R17 105S'
'P265/70R16 111S'
'P275/55R20 111H'
'285/60R18 120H'
'P235/70R17 108S'
What I need:
'P245/' '65' 'R' '17' '105' 'S'
'P265/' '70' 'R' '16' '111' 'S'
'P275/' '55' 'R' '20' '111' 'H'
'285/' '60' 'R' '18' '120' 'H'
'P235/' '70' 'R' '17' '108' 'S'
Thanks in advance!
Accepted Answer
More Answers (1)
data = {'P245/65R17 105S';
'P265/70R16 111S';
'P275/55R20 111H';
'285/60R18 120H';
'P235/70R17 108S'};
splitdata = regexp(data, '(.+/)(\d+)([A-Z])(\d+) (\d+)([A-Z])', 'tokens', 'once');
splitdata = vertcat(splitdata{:})
The regular expression is divided into tokens (the () in the regex)
- the 1st token is one or more (the +) character (the .) followed by '/'
- the 2nd token is one or more (the +) digit (the \d)
- the 3rd token is a single character between A and Z (the [A-Z])
- 4th token, see 2nd
- it then matches a space which is not part of any token
- 5th token, see 2nd
- 6th token, see 3rd
3 Comments
Star Strider
on 12 Nov 2015
Great documentation of the regexp call!
Aldrich To
on 12 Nov 2015
Guillaume
on 13 Nov 2015
A regex that would most likely work with all your cases would be
regexp(data, '([A-Z]*)(\d+)(/)(\d+)([A-Z])(\d+) (\d+(/\d+)?)?([A-Z])', 'tokens')
Categories
Find more on Cell 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!