Selecting numbers out of a text file?

1 次查看(过去 30 天)
So I have a text file with a list of scores, I have used strtok to get the text file to just show up as numbers in a list separated by commas, how do I go from this list to just an array of the numbers. for example
'0,1,2,3' '9,8.2,5,6' '4,8,4,5' '.2,4,3,2' to [0 1 2 3 9 8.2 5 6 4 8 4 5 .2 4 3 2]

回答(1 个)

Jonathan Chin
Jonathan Chin 2017-10-12
编辑:Jonathan Chin 2017-10-12
Take a look at str2num
txt='0,1,2,3,9,8.2,5,6,4,8,4,5,.2,4,3,2';
numArray=str2num(txt)
if txt is a cell array you can use cellfun and str2num
txt={'0,1,2,3' '9,8.2,5,6' '4,8,4,5' '.2,4,3,2'}
tmp=cellfun(@str2num,txt,'UniformOutput',false)
numArray = [tmp{:}]

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by