f



Reading a text file into a Matlab Matrix - text file contains strings

Hello there,

I have a text file that contains the following;

word11 word12 word13 word14
word21 word22 word23 word24
word31 word32 word33 word34

I want to read it into a 3x4 matrix (each word in a separate cell)

I tried to use;

data = textread('fileName.txt', '%s', 'delimiter', '\n')  - puts each line in one cell

data = textread('fileName.txt', '%s', 'delimiter', ' ') - puts each word in one cell but in a 12x1 matrix/array

[col1 col2 col3 col4] = textread('tE.txt', '%s %s %s %s', 'delimiter', ' ')  - creates 4 different arrays.

My problem is that I need one matrix that contains all the data.

Thank you.

Roronoa Zoro
0
Ayman
9/12/2010 4:09:04 PM
comp.soft-sys.matlab 211266 articles. 23 followers. lunamoonmoon (257) is leader. Post Follow

5 Replies
1589 Views

Similar Articles

[PageSpeed] 24

On 12/09/10 11:09 AM, Ayman Hajja wrote:

> I have a text file that contains the following;
>
> word11 word12 word13 word14
> word21 word22 word23 word24
> word31 word32 word33 word34
>
> I want to read it into a 3x4 matrix (each word in a separate cell)

> [col1 col2 col3 col4] = textread('tE.txt', '%s %s %s %s', 'delimiter', '
> ') - creates 4 different arrays.

What does

[col1 col2 col3 col4]

then give you?
0
Walter
9/12/2010 4:39:39 PM
Walter Roberson <roberson@hushmail.com> wrote in message <gn7jo.22229$y85.14114@newsfe13.iad>...
> On 12/09/10 11:09 AM, Ayman Hajja wrote:
> 
> > I have a text file that contains the following;
> >
> > word11 word12 word13 word14
> > word21 word22 word23 word24
> > word31 word32 word33 word34
> >
> > I want to read it into a 3x4 matrix (each word in a separate cell)
> 
> > [col1 col2 col3 col4] = textread('tE.txt', '%s %s %s %s', 'delimiter', '
> > ') - creates 4 different arrays.
> 
> What does
> 
> [col1 col2 col3 col4]
> 
> then give you?

col1 will give me a 3x1 array, same for col2, col3 and col4.



I can code;

all = [col1, col2, col3, col4]           and I would get exactly what I need (a 3x4 matrix) . However, what if I have so many columns, let's say 20!! There must be a way to do it without specifying names for the columns!!

Thank you.

Zoro
0
Ayman
9/12/2010 5:01:04 PM
"Ayman Hajja" <amhajja@gmail.com> wrote in message <i6j10g$gnp$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <gn7jo.22229$y85.14114@newsfe13.iad>...

SNIP

Hi Zoro,
What about to write

d = reshape(textread('words.txt', '%s'),4,3)'

If  you have a mztrix of the type (m,n), you could write

d = reshape(textread('words.txt', '%s'),n,m)'

Best regards
Mira
0
Miroslav
9/12/2010 5:16:04 PM
On 12/09/10 12:01 PM, Ayman Hajja wrote:

>> > [col1 col2 col3 col4] = textread('tE.txt', '%s %s %s %s',
>> 'delimiter', '
>> > ') - creates 4 different arrays.

> I can code;
>
> all = [col1, col2, col3, col4] and I would get exactly what I need (a
> 3x4 matrix) . However, what if I have so many columns, let's say 20!!
> There must be a way to do it without specifying names for the columns!!

Use textscan() instead of textread(); it will return a cell row vector, 
each entry of which contains all the data for one column. If that array 
is C, then horzcat(C{:}) should get you the matrix you are looking for.
0
Walter
9/12/2010 5:37:29 PM
"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <i6j1sk$cs4$1@fred.mathworks.com>...
> "Ayman Hajja" <amhajja@gmail.com> wrote in message <i6j10g$gnp$1@fred.mathworks.com>...
> > Walter Roberson <roberson@hushmail.com> wrote in message <gn7jo.22229$y85.14114@newsfe13.iad>...
> 
> SNIP
> 
> Hi Zoro,
> What about to write
> 
> d = reshape(textread('words.txt', '%s'),4,3)'
> 
> If  you have a mztrix of the type (m,n), you could write
> 
> d = reshape(textread('words.txt', '%s'),n,m)'
> 
> Best regards
> Mira

Mira. Your answer is  beautiful. It worked perfectly. I had to get the transpose of the answer matrix though. Thank you.

Walter; Your answer also worked perfectly. Thank you.
0
Ayman
9/12/2010 6:04:03 PM
Reply: