I need to separate 3 conjoined values into 3 separate values
古いコメントを表示
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 件のコメント
James Tursa
2017 年 3 月 8 日
It doesn't look like a column. Is this a string that you need parsed into numbers?
Adam
2017 年 3 月 8 日
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
2017 年 3 月 8 日
回答 (1 件)
Star Strider
2017 年 3 月 9 日
編集済み: Star Strider
2017 年 3 月 9 日
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.
カテゴリ
ヘルプ センター および File Exchange で String Parsing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!