Got Questions? Get Answers.
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:
Creating a matrix of strings?

Subject: Creating a matrix of strings?

From: Tomaz

Date: 16 Apr, 2010 07:50:08

Message: 1 of 4

Hi!
I am trying to store some words into a vector/ matrix. I noticed that they are treated as chars in Matlab. So, my questions is: is it possible to get a particular word (string) instead of char?

To be more clear, here is example:
columnNames = ['TOTSPACE','LIVSPACE','KITSPACE','DISTKM','ROOMS','BRICK'];

I would like to get TOTSPACE when I type in columnNames(1). Currently I am getting 'T'. Is there any trick/ method to do this?

Subject: Creating a matrix of strings?

From: Tomaz

Date: 16 Apr, 2010 08:05:06

Message: 2 of 4

"Tomaz " <tomaz.bartolj@gmail.com> wrote in message <hq94rg$l1q$1@fred.mathworks.com>...
> Hi!
> I am trying to store some words into a vector/ matrix. I noticed that they are treated as chars in Matlab. So, my questions is: is it possible to get a particular word (string) instead of char?
>
> To be more clear, here is example:
> columnNames = ['TOTSPACE','LIVSPACE','KITSPACE','DISTKM','ROOMS','BRICK'];
>
> I would like to get TOTSPACE when I type in columnNames(1). Currently I am getting 'T'. Is there any trick/ method to do this?

OK, I can answer my own question (just in case somebody else will be looking for solution as well). You put strings into cells and than read specific cell value:

columnNames = {'TOTSPACE','LIVSPACE','KITSPACE','DISTKM','ROOMS','BRICK'};
someText = columnNames{1,i};

Subject: Creating a matrix of strings?

From: Tomaz

Date: 16 Apr, 2010 08:08:04

Message: 3 of 4

"Tomaz " <tomaz.bartolj@gmail.com> wrote in message <hq94rg$l1q$1@fred.mathworks.com>...
> Hi!
> I am trying to store some words into a vector/ matrix. I noticed that they are treated as chars in Matlab. So, my questions is: is it possible to get a particular word (string) instead of char?
>
> To be more clear, here is example:
> columnNames = ['TOTSPACE','LIVSPACE','KITSPACE','DISTKM','ROOMS','BRICK'];
>
> I would like to get TOTSPACE when I type in columnNames(1). Currently I am getting 'T'. Is there any trick/ method to do this.

OK, I can answer my own question (in case somebody else will be looking for solution too). You store strings into cell array and than look at value of specific cell.

columnNames = {'TOTSPACE','LIVSPACE','KITSPACE','DISTKM','ROOMS','BRICK'};
someText = columnNames{1,i};

Subject: Creating a matrix of strings?

From: James Tursa

Date: 16 Apr, 2010 08:10:12

Message: 4 of 4

"Tomaz " <tomaz.bartolj@gmail.com> wrote in message <hq94rg$l1q$1@fred.mathworks.com>...
> Hi!
> I am trying to store some words into a vector/ matrix. I noticed that they are treated as chars in Matlab. So, my questions is: is it possible to get a particular word (string) instead of char?
>
> To be more clear, here is example:
> columnNames = ['TOTSPACE','LIVSPACE','KITSPACE','DISTKM','ROOMS','BRICK'];
>
> I would like to get TOTSPACE when I type in columnNames(1). Currently I am getting 'T'. Is there any trick/ method to do this?

You could use cell arrays for this. e.g.,

columnNames = {'TOTSPACE','LIVSPACE','KITSPACE','DISTKM','ROOMS','BRICK'};

Then to get the contents of each cell, use the curly braces instead of the brackets. e.g.,

columnNames{1}

James Tursa

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