How to extract only numerical values from cell into new column array?
Afficher commentaires plus anciens
For this data:
"chb12_06.edf"
"1665 seconds"
"1726 seconds"
"3415 seconds"
"3447 seconds"
"chb12_08.edf"
"1426 seconds"
"1439 seconds"
"1591 seconds"
"1614 seconds"
"1957 seconds"
"1977 seconds"
"2798 seconds"
"2824 seconds"
"chb12_09.edf"
"3082 seconds"
"3114 seconds"
"3503 seconds"
"3535 seconds"
"chb12_10.edf"
"593 seconds"
"625 seconds"
"811 seconds"
"856 seconds"
Whis is a column in a larger cell array. I want to extract just the seconds value after each edf file, and possibly put them in a new array or matrix, eg:
{"chb12_06.edf" "1665 seconds" "1726 seconds" "3415 seconds" "3447 seconds" ; "chb12_08.edf" "1426 seconds" "1439 seconds" "1591 seconds" "1614 seconds" "1957 seconds" "1977 seconds" "2798 seconds" "2824 seconds"...}
I hope this makes sense what I'm trying to achieve, if not please feel free to clarify, thanks!
Réponse acceptée
Plus de réponses (1)
Stephane Dauvillier
le 15 Juil 2019
Try this:
in=["chb12_06.edf"
"1665 seconds"
"1726 seconds"
"3415 seconds"
"3447 seconds"
"chb12_08.edf"
"1426 seconds"
"1439 seconds"
"1591 seconds"
"1614 seconds"
"1957 seconds"
"1977 seconds"
"2798 seconds"
"2824 seconds"
"chb12_09.edf"
"3082 seconds"
"3114 seconds"
"3503 seconds"
"3535 seconds"
"chb12_10.edf"
"593 seconds"
"625 seconds"
"811 seconds"
"856 seconds"]
notQuiteThat = regexp(in,"(\d+) second","once","tokens") % extarct only digits befor second
notQuiteThat = [notQuiteThat{:}] % delete empty candidate
theSeconds = str2double(notQuiteThat) % transform it into actual double value
3 commentaires
madhan ravi
le 15 Juil 2019
Modifié(e) : madhan ravi
le 15 Juil 2019
+1, note the data is a cell array not string array.
notQuiteThat = regexp(string(in),"(\d+) second","once","tokens")
As an alternative:
v=string(V); % V - data
sscanf(char(v(contains(v,'seconds'))),'%d seconds')
Z Liang
le 15 Juil 2019
Stephen23
le 15 Juil 2019
"..what does the parenthesis do in this instance?"
Read about tokens in regular expressions:
Catégories
En savoir plus sur Cell Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!