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:
'subsindex' error when creating a structure using struct

Subject: 'subsindex' error when creating a structure using struct

From: Kathryn

Date: 25 Aug, 2010 20:26:19

Message: 1 of 4

Hello,

I am looking at the "Getting started" documentation for structures, and it indicates that structures can be created by one of two methods. The first is to add values to individual fields, and the second is to use the struct function.

I have been successful in creating a structure s by addressing individual fields, like so:

s.a = 5
s.b= 10
s.c = 15

and can even add other elements to my structure:

s(2).a = 7

But every time I try to use the struct function to create a structure, I receive an error, even when I cut and paste commands from the help file!

s = struct('a',5,'b',10,'c',15)
??? Index exceeds matrix dimensions.

-or-

 s = struct('a', {{1 4 7 2 9 3}, 'Anne'}, ...
           'b', {'James', pi}, ...
           'c', {magic(3), (1:7)'});
??? Error using ==> subsindex
Function 'subsindex' is not defined for values of class 'cell'.

What is happening here? Is the struct command being phased out? I am using the R2010a distribution of MATLAB.

Before I started digging in the documentation, I wanted to know whether it is possible to add element-wise to an structure, rather than field-wise, with the struct command. But now I am frustrated and disinclined to continue reading documentation on the subject, so if you have any help about this question I'd appreciate hearing about that too. Thanks for reading.

Subject: 'subsindex' error when creating a structure using struct

From: someone

Date: 25 Aug, 2010 20:45:06

Message: 2 of 4

"Kathryn " <mccormick@uoneuro.uoregon.edu> wrote in message <i53u9b$r81$1@fred.mathworks.com>...
> Hello,
>
> I am looking at the "Getting started" documentation for structures, and it indicates that structures can be created by one of two methods. The first is to add values to individual fields, and the second is to use the struct function.
>
> I have been successful in creating a structure s by addressing individual fields, like so:
>
> s.a = 5
> s.b= 10
> s.c = 15
>
> and can even add other elements to my structure:
>
> s(2).a = 7
>
> But every time I try to use the struct function to create a structure, I receive an error, even when I cut and paste commands from the help file!
>
> s = struct('a',5,'b',10,'c',15)
> ??? Index exceeds matrix dimensions.
>
> -or-
>
> s = struct('a', {{1 4 7 2 9 3}, 'Anne'}, ...
> 'b', {'James', pi}, ...
> 'c', {magic(3), (1:7)'});
> ??? Error using ==> subsindex
> Function 'subsindex' is not defined for values of class 'cell'.
>
> What is happening here? Is the struct command being phased out? I am using the R2010a distribution of MATLAB.
>
> Before I started digging in the documentation, I wanted to know whether it is possible to add element-wise to an structure, rather than field-wise, with the struct command. But now I am frustrated and disinclined to continue reading documentation on the subject, so if you have any help about this question I'd appreciate hearing about that too. Thanks for reading.

The above commands work for me.
(I am using MATLAB R2007b right now.)

Have you by any chance, created a fuction called struct that is overriding the MATLAB struct function?

Type

which struct

on the MATLAB command line to see. You should get something like:

built-in (C:\Program Files\MATLAB\R2007b\toolbox\matlab\datatypes\struct)

Subject: 'subsindex' error when creating a structure using struct

From: Walter Roberson

Date: 25 Aug, 2010 20:52:29

Message: 3 of 4

On 10-08-25 03:26 PM, Kathryn wrote:

> I am looking at the "Getting started" documentation for structures, and
> it indicates that structures can be created by one of two methods. The
> first is to add values to individual fields, and the second is to use
> the struct function.
> I have been successful in creating a structure s by addressing
> individual fields, like so:
>
> s.a = 5
> s.b= 10
> s.c = 15
>
> and can even add other elements to my structure:
>
> s(2).a = 7
>
> But every time I try to use the struct function to create a structure, I
> receive an error, even when I cut and paste commands from the help file!
>
> s = struct('a',5,'b',10,'c',15)
> ??? Index exceeds matrix dimensions.

It appears to me that you have either defined a variable named 'struct' or
else you have defined your own function named 'struct'.

Try

which struct

Subject: 'subsindex' error when creating a structure using struct

From: Kathryn McCormick

Date: 25 Aug, 2010 21:12:06

Message: 4 of 4


>
> It appears to me that you have either defined a variable named 'struct' or
> else you have defined your own function named 'struct'.
>
> Try
>
> which struct

You are very right. Oops! Thanks and sorry for the trouble.

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