• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Matlab : How Can I access arrays in a structure through indices?

I have a structure that I have loaded with 1xn arrays of elements. Let call the structure 'cat'.

I want to access the ith element of cat without knowing the name of that element. I've been playing around with it but I can't seem to do it. This would be extremely useful because then i wouldn't have to fall back to using eval() which makes everything more tedious for me.
 
I am assuming by 'name of the element' you actually mean the field name.

fn=cell2mat(fieldnames(cat));

The command fieldnames returns the names of the field.

The returned value is stored as a cell which is not useful, cell2mat command was used to convert the value in the cell to a string, so now you have a string variable fn.
 
so yesterday while flipping through the mathworks online help I found the same thing, but this still creates many problems because to access those arrays stored in the structure I'd have to use the eval command.

fn, of what you have listed, will give me the field names. But I don't necessarily want the field names; I just want to access the data in these arrays using an index.

What I want to do is:
cat=[ 1 23 43 5]

cat(1) = 1
cat(2) = 23

If I had to determine the syntax myself...

cat.firstArray= [1 2 3 4 5 6]
cat.secondArray=[ 2 5 6 7 7 9]

using only index, I'd love to access the data in second array.

Something like cat(2) would hopefully return [2 5 6 7 7 9].

If I went with the field names then I'd have to use eval

eval([ 'array = cat.' fn(2) ]) would spit out the array. That doesn't seem that bad, but I'm going to be doing plenty of plotting and this can easily turn into hell lol.


if I can access the data through an index point, then given an arbitrary number of arrays stored in a structure I can easily start to cycle through them with a for loop where the looping "i" will help me grab out what I want....
 
Ok, try this instead.

cell_cat=struct2cell(cat);
mat_cat=cell2mat(cell_cat);

mat_cat contains all the arrays arranged row wise.

mat_cat(1,: ) returns cat.firstArray and so on..

Keep in mind, these set of commands will work when all the elements are arrays and have the same length.

Or, you can work directly with cell_cat. I don't remember exactly the commands related to working with cells. The advantage of working with cells is they are dimension invariant.
 
Last edited:
Ok, try this instead.

cell_cat=struct2cell(cat);
mat_cat=cell2mat(cell_cat);

mat_cat contains all the arrays arranged row wise.

mat_cat(1,: ) returns cat.firstArray and so on..

Keep in mind, these set of commands will work when all the elements are arrays and have the same length.

Or, you can work directly with cell_cat. I don't remember exactly the commands related to working with cells. The advantage of working with cells is they are dimension invariant.


Lemme give it a shot.

Yup they are all the same length.

I was actually thinking about the nature of a structure and slowly started to realize I couldn't manipulate it directly the way I wanted to because the struct can contain so many different types and lengths of data.
 
works wonderfully. I appreciate it. I'm glad I could keep it all stored as a structure and then call it out when i need it.

Glad I could help.

I also found this, which may be of interest, since it pretty much also covers this. http://blogs.mathworks.com/pick/2008...res-of-arrays/ Too bad there are rarely any matlab questions around here...I'd try to give back haha

Thanks, bookmarked for later. Nothing wrong in learning something new.
 
If they're all the same length, a 2-d array is all you need. If you want to use a cell array for some reason, it will add some overhead which may or may not be important, depending on the amount of number crunching you're doing.
 
just an off suggestion... if all your arrays are the same size, why not change it into an array of structs.

struct element
{
int first;
int second;
int third;
};


int X = 10;
element *head_ptr;
head_ptr = new element[X];

you can now access all three elements (first second third) with just the Ith position via:

head_ptr[5].first
head_ptr[5].second
head_ptr[5].third


obviously this has some limitations, like the ability to simultaniously sort multiple arrays... but I dont know what you are actually going to do with it.
 
Back
Top