Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Read from file into matrix
Replies: 1   Last Post: May 5, 2013 4:54 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Nasser Abbasi

Posts: 5,673
Registered: 2/7/05
Re: Read from file into matrix
Posted: May 5, 2013 4:54 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.