Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Reading a text file into a Matlab Matrix - text file contains strings

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

From: Ayman Hajja

Date: 12 Sep, 2010 16:09:04

Message: 1 of 6

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

Subject: Reading a text file into a Matlab Matrix - text file contains

From: Walter Roberson

Date: 12 Sep, 2010 16:39:39

Message: 2 of 6

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?

Subject: Reading a text file into a Matlab Matrix - text file contains

From: Ayman Hajja

Date: 12 Sep, 2010 17:01:04

Message: 3 of 6

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

Subject: Reading a text file into a Matlab Matrix - text file contains

From: Miroslav Balda

Date: 12 Sep, 2010 17:16:04

Message: 4 of 6

"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

Subject: Reading a text file into a Matlab Matrix - text file contains

From: Walter Roberson

Date: 12 Sep, 2010 17:37:29

Message: 5 of 6

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.

Subject: Reading a text file into a Matlab Matrix - text file contains

From: Ayman Hajja

Date: 12 Sep, 2010 18:04:03

Message: 6 of 6

"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.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us