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:
about joining arrays with strings and numbers

Subject: about joining arrays with strings and numbers

From: Sris P

Date: 25 Oct, 2010 13:51:04

Message: 1 of 3

Hello,

This might be a simple problem but I haven't been able to find any help online. Ok I have two arrays:

A = [1 2 3 4];
B = ['cat' 'dog' 'bird' 'cow'];

what i want to do is something like this:

AB = [1 2 3 4
         cat dog bird cow]

but matlab is not letting me do this because one is a set of numbers and the other is a set of strings

any help?
cheers,
S.

Subject: about joining arrays with strings and numbers

From: Enrique Vidal Sanchez

Date: 25 Oct, 2010 14:05:06

Message: 2 of 3

On Oct 25, 3:51 pm, "Sris P" <nepalingrap...@yahoo.com> wrote:
> Hello,
>
> This might be a simple problem but I haven't been able to find any help online. Ok I have two arrays:
>
> A = [1 2 3 4];
> B = ['cat' 'dog' 'bird' 'cow'];
>
> what i want to do is something like this:
>
> AB = [1     2    3     4
>          cat dog bird cow]
>
> but matlab is not letting me do this because one is a set of numbers and the other is a set of strings
>
> any help?
> cheers,
> S.

You may want to use cells:

a = {1 2 3 4};
b = {'cat' 'dog' 'bird' 'cow'};
ab = [a;b]

ab =
    [ 1] [ 2] [ 3] [ 4]
    'cat' 'dog' 'bird' 'cow'

For example, column 2 gives:
ab(:,2)
ans =
    [2]
    'dog'

Regards,
Enrique Vidal

Subject: about joining arrays with strings and numbers

From: Sean

Date: 25 Oct, 2010 14:06:03

Message: 3 of 3

"Sris P" <nepalingraphix@yahoo.com> wrote in message <ia4208$8h8$1@fred.mathworks.com>...
> Hello,
>
> This might be a simple problem but I haven't been able to find any help online. Ok I have two arrays:
>
> A = [1 2 3 4];
> B = ['cat' 'dog' 'bird' 'cow'];
>
> what i want to do is something like this:
>
> AB = [1 2 3 4
> cat dog bird cow]
>
> but matlab is not letting me do this because one is a set of numbers and the other is a set of strings
>
> any help?
> cheers,
> S.

You'll have to use cell arrays:
B = {'cat' 'dog' 'bird' 'cow'}
AB = [{A};{B}];

or
A = {1 2 3 4};
AB = [A; B];

depending on what you want

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