Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.
|
|
|
Re: Read from file into matrix
Posted:
May 5, 2013 4:54 AM
|
|
On 5/5/2013 3:17 AM, Gabriel wrote: > I have a file with entries like > 0, "150 145 2 55 13 54 ..." > There are 2304 numbers in quotes. And I have more than 22 000 of such entries. > So I need to read them from file one by one. > How can I do this? >
You can try using textscan, using '%d%s' format.
One you read the 2 fields, you can then break the second field (the string) into its parts using something like strread(). Like this
------------------------- s='150 145 2 55 13 54' c=strread(s,'%s','delimiter',' ')
'150' '145' '2' '55' '13' '54' ------------------------
once you have the parts broken, you can now easily convert them to numbers, say using
cell2mat(cellfun(@str2num,c, 'UniformOutput',false))
ans =
150 145 2 55 13 54
So, you have everything you need to build your matrix from what is in the file.
--Nasser
|
|
|
|