Seperate numbers in string to different array
1 次查看(过去 30 天)
显示 更早的评论
I import a text file using textscan and use vertcat to assign each line of the file its own cell. I am working with data to where it says something like: take off time 0.102 sec sensor1 initiated .361 sec, -31 (mv) sync at 50 ms and this continues for about 42 rows of data. My end goal is to separate all the numeric data from the words. For example have an array that would show [.102; .361 -31; 50;....]. I have tried regexp and sscanf but have had no luck.
0 个评论
回答(2 个)
Azzi Abdelmalek
2016-9-7
str={'take off time 0.102 sec';'sensor1 initiated .361 sec, -31 (mv)';'sync at 50 ms'}
out=regexp(str,'\s[-\d\.]?\.?\d+','match')
out=str2double([out{:}])
2 个评论
Star Strider
2016-9-7
See if this does what you want:
str = 'take off time 0.102 sec sensor1 initiated .361 sec, -31 (mv) sync at 50 ms';
nbrvct = cellfun(@str2num, regexp(str, '\d+\.\d+|\.\d+|\-\d+|\d+', 'match'))
nbrvct =
0.102 1 0.361 -31 50
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!