I need to separate 3 conjoined values into 3 separate values
Show older comments
I have a column of conjoined values (27.525.525.0) which I need to split into separate values (27.5, 25.5, 25.0) so they can be graphed. Thank you for any help!
3 Comments
James Tursa
on 8 Mar 2017
It doesn't look like a column. Is this a string that you need parsed into numbers?
Adam
on 8 Mar 2017
There also isn't a unique way to separate that conjoined 'thing' into 3 values unless there are rules to go with it e.g.
27.52, 5.52, 5.0
would equally be a separation into 3 values.
Greig
on 8 Mar 2017
Answers (1)
Star Strider
on 9 Mar 2017
Edited: Star Strider
on 9 Mar 2017
If you read them in as strings, you can parse them into individual strings with the appropriate numbers with a simple regexp call. Then convert them to a numeric array:
T = '27.525.525.0'; % String Input
NrCell = regexp(T, '\d{2}\.\d', 'match') % Parse
NrVctr = cellfun(@str2double, NrCell) % Convert To Numeric Vector
NrCell =
1×3 cell array
'27.5' '25.5' '25.0'
NrVctr =
27.5 25.5 25
We do finally end up with a numeric (1x3) row vector.
This will require a loop to send each string to regexp (it cannot read a column matrix of strings), then store ‘NrVctr’ as a row in a matrix.
EDIT — Made ‘NrVctr’ more efficient, corrected ambiguities in description.
Categories
Find more on String Parsing 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!