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:
struct for cell value substitution

Subject: struct for cell value substitution

From: Kirk

Date: 31 Oct, 2011 20:45:33

Message: 1 of 5

I am looking for advice on how to substitute structures for numerical cell values in an array (essentially creating an array of structures). For example, let's say you have an array:

array1 = [1 2; 1 1];

and two structures S and SS:

S.name = 'bob';
S.height = 1.8;
S.weight = 70;

SS.name = 'paula'
SS.height = 1.5
SS.weight = 55;

The goal is to substitue 'S' for the value 1 in array1, and 'SS' for the value 2 in array1.

Any ideas would be appreciated.

Subject: struct for cell value substitution

From: Kirk

Date: 31 Oct, 2011 22:53:11

Message: 2 of 5

"Kirk" <kwythers.nospam@umn.edu> wrote in message <j8n1dc$324$1@newscl01ah.mathworks.com>...
> I am looking for advice on how to substitute structures for numerical cell values in an array (essentially creating an array of structures). For example, let's say you have an array:
>
> array1 = [1 2; 1 1];
>
> and two structures S and SS:
>
> S.name = 'bob';
> S.height = 1.8;
> S.weight = 70;
>
> SS.name = 'paula'
> SS.height = 1.5
> SS.weight = 55;
>
> The goal is to substitue 'S' for the value 1 in array1, and 'SS' for the value 2 in array1.
>
> Any ideas would be appreciated.

The main issue seems to be converting from a double to struct. For example:

 test(test == 1) = S;

??? The following error occurred converting from struct to
double:
Error using ==> double
Conversion to double from struct is not possible.

However, you can replace the ones with fives by using:

test(test==1) = 5

Subject: struct for cell value substitution

From: ade77

Date: 31 Oct, 2011 23:37:11

Message: 3 of 5

"Kirk" <kwythers.nospam@umn.edu> wrote in message <j8n1dc$324$1@newscl01ah.mathworks.com>...
> I am looking for advice on how to substitute structures for numerical cell values in an array (essentially creating an array of structures). For example, let's say you have an array:
>
> array1 = [1 2; 1 1];
>
> and two structures S and SS:
>
> S.name = 'bob';
> S.height = 1.8;
> S.weight = 70;
>
> SS.name = 'paula'
> SS.height = 1.5
> SS.weight = 55;
>
> The goal is to substitue 'S' for the value 1 in array1, and 'SS' for the value 2 in array1.
>
> Any ideas would be appreciated.

The problem is confusing. What will be the final result assuming everything works correctly

Subject: struct for cell value substitution

From: Kirk

Date: 1 Nov, 2011 17:28:26

Message: 4 of 5

"ade77 " <ade100a@gmail.com> wrote in message <j8nbf7$3p4$1@newscl01ah.mathworks.com>...
> "Kirk" <kwythers.nospam@umn.edu> wrote in message <j8n1dc$324$1@newscl01ah.mathworks.com>...
> > I am looking for advice on how to substitute structures for numerical cell values in an array (essentially creating an array of structures). For example, let's say you have an array:
> >
> > array1 = [1 2; 1 1];
> >
> > and two structures S and SS:
> >
> > S.name = 'bob';
> > S.height = 1.8;
> > S.weight = 70;
> >
> > SS.name = 'paula'
> > SS.height = 1.5
> > SS.weight = 55;
> >
> > The goal is to substitue 'S' for the value 1 in array1, and 'SS' for the value 2 in array1.
> >
> > Any ideas would be appreciated.
>
> The problem is confusing. What will be the final result assuming everything works correctly


Here is a graphic example of what I'm trying to do. Hopefully that will make the problem more clear.

Let's say I have a matrix 'M' of doubles that looks like this:

0 1 0 1
0 2 1 0
0 0 1 0
1 0 2 0

and I want to replace with values '1' with the struct 'S', so that the final result looks like:

0 S 0 S
0 2 S 0
0 0 S 0
S 0 2 0

Does that make the problem more clear? I tried to simply replace the doubles with the struct like this (got the error that conversion from double to struct is not possible):

M(M ==1) = S;
??? The following error occurred converting from struct to double:
Error using ==> double
Conversion to double from struct is not possible.

Subject: struct for cell value substitution

From: Kirk

Date: 1 Nov, 2011 17:29:14

Message: 5 of 5

"ade77 " <ade100a@gmail.com> wrote in message <j8nbf7$3p4$1@newscl01ah.mathworks.com>...
> "Kirk" <kwythers.nospam@umn.edu> wrote in message <j8n1dc$324$1@newscl01ah.mathworks.com>...
> > I am looking for advice on how to substitute structures for numerical cell values in an array (essentially creating an array of structures). For example, let's say you have an array:
> >
> > array1 = [1 2; 1 1];
> >
> > and two structures S and SS:
> >
> > S.name = 'bob';
> > S.height = 1.8;
> > S.weight = 70;
> >
> > SS.name = 'paula'
> > SS.height = 1.5
> > SS.weight = 55;
> >
> > The goal is to substitue 'S' for the value 1 in array1, and 'SS' for the value 2 in array1.
> >
> > Any ideas would be appreciated.
>
> The problem is confusing. What will be the final result assuming everything works correctly


Here is a graphic example of what I'm trying to do. Hopefully that will make the problem more clear.

Let's say I have a matrix 'M' of doubles that looks like this:

0 1 0 1
0 2 1 0
0 0 1 0
1 0 2 0

and I want to replace with values '1' with the struct 'S', so that the final result looks like:

0 S 0 S
0 2 S 0
0 0 S 0
S 0 2 0

Does that make the problem more clear? I tried to simply replace the doubles with the struct like this (got the error that conversion from double to struct is not possible):

M(M ==1) = S;
??? The following error occurred converting from struct to double:
Error using ==> double
Conversion to double from struct is not possible.

Tags for this Thread

No tags are associated with 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