Two mex files: structindex selects elements from all fields of a structure ....
Updated 20 Apr 2005

No License

This is for those people who like structures of arrays (rather than arrays of structures). They tend to be faster, and require less memory.

There are two functions in this package. "structindex" selects elements from all fields of a structure, while "structcat" concatinates two (or more) like structures.

I've included the MEX files for Windows (Matlab ver 6.5?). To compile it for your platform, try
mex structindex.c
mex structcat.c

STRUCTINDEX Extract data elements from a structure of arrays.
STRUCTINDEX(S,INDEX) returns selected data elements from each field of S. The returned value has all the same fields as S, and each field will have as many elements as are in INDEX. INDEX is an array of indecies to select. If a given index does not exist for a particular field of S, a default value for the data element is used.

Currently, indexing in one-dimensional only (fields having data of two or more dimensions are not supported). Also, indexing through the use use of logical arrays is not supported at this time.

EXAMPLES: = {'sam','mike','john'};
s.income = [10000,90000,-10];
ans =
name: {'mike' 'john'}
income: [90000 -10]
structindex(s,[2 2 2 99])
Warning: Index vector contained values exceeding structure field sizes
ans =
name: {'mike' 'mike' 'mike' []}
income: [90000 90000 90000 0]

See also STRUCTCAT.%STRUCTCAT Concatination of fields of two or more structures.

STRUCTCAT(S1,S2,...) returns selected data elements from each field. The returned structure will have all fields that are contained in the input structures. Each field will be the (one-dimensional) concatination of all data from the input structures containging that field.

EXAMPLES: = {'sam','mike','john'};
s.income = [10000,90000,-10]; = {'julie','carol'};
g.income = [15000,120000];
g.age = [19 64];
ans =
name: {'sam' 'mike' 'john' 'julie' 'carol'}
income: [10000 90000 -10 15000 120000]
age: [19 64]


Cite As

AJ Johnson (2024). structindex (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Structures in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes