Selecting numbers out of a text file?

1 view (last 30 days)
Lauren Harkness
Lauren Harkness on 12 Oct 2017
Edited: Jonathan Chin on 12 Oct 2017
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]

Answers (1)

Jonathan Chin
Jonathan Chin on 12 Oct 2017
Edited: Jonathan Chin on 12 Oct 2017
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{:}]

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!