Remove String and extract numbers to move to new column

1 view (last 30 days)
I have a column like that:
WPS
"[0, 0]"
"[0, 5]"
"[5, 115]"
"[115, 219]"
"[219, 262]"
"[262, 328]"
"[328, 408]"
"[408, 424]"
"[424, 531]"
"[531, 562]"
Now for me to be able to further work with that, I would like to move the first and the second number in each of the strings to seperate new columns, which should look like that:
WP1 WP2
0 0
0 5
5 115
115 219
219 262
262 328
328 408
408 424
424 531
531 562
Now I found out that it somehow can be done with regular expressions, but I can not really figure out how. I'm thinking I need to use str2num but this would not work, as my string still contains brackets - any hint is appreciated - thank you!

Accepted Answer

Stephen23
Stephen23 on 4 Aug 2021
S = [...
"[0, 0]"
"[0, 5]"
"[5, 115]"
"[115, 219]"
"[219, 262]"
"[262, 328]"
"[328, 408]"
"[408, 424]"
"[424, 531]"
"[531, 562]"];
M = sscanf([S{:}],'[%f,%f]',[2,Inf]).'
M = 10×2
0 0 0 5 5 115 115 219 219 262 262 328 328 408 408 424 424 531 531 562

More Answers (0)

Community Treasure Hunt

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

Start Hunting!