How do i define a structure in Matlab

I know that a structure can be defined by in several ways such as:

  1. Adding fields to a variable

    p.color.red = .2;
    p.color.green = .4;
    p.color.blue = .7;
    
  2. Defining a scalar structure by assignment

    S = struct('a',  0, 'b',  1, 'c',  2);
    

What I want to be able to do is create a structure definition (like C). My end goal is to have an array of structures that i can iterate through and perform testing on. Is there any way I can define a generic structure and then create instances of it? Should I use some other mechanism. I know MATLAB supports Java, should I use a class/interface?

Answers


Unlike C, Matlab allows you to add or remove members from a struct as you go. It's more of a convention that a struct with certain fields can be used with certain functions.

So yes, those are the two basic ways to create structures. Of course, you can always write a function in a *.m file which creates a structure with certain fields for you. Or if you want to get more OO, see here


You mentioned your goal was to work with several structure of the same type, therefore you should be aware of the main two approaches available to you and how they compare:

1) array of structures

You can initialize it by growing the array dynamically

p(1).str = 'white';
p(1).r = 1;
p(1).g = 1;
p(1).b = 1;
p(2).str = 'black';
p(2).r = 0;
p(2).g = 0;
p(2).b = 0;

However its always better to start by pre-allocating the array

p = repmat( struct('r',[], 'g',[], 'b',[], 'str',[]), 1, 10);

another trick to pre-allocate:

p(10) = struct('r',[], 'g',[], 'b',[], 'str',[]);

Or even give all values at initialization:

p = struct('r',{1 0}, 'g',{1 0}, 'b',{1 0}, 'str',{'white' 'black'});

A simple way to fill values

names = {'white' 'black'};
[p(1:2).str] = names{:};
red = num2cell([1 0]);
[p(1:2).r] = red{:};

Here's how you retrieve all values of one field:

red = [p(:).r];
names = {p(:).str};
2) structures of arrays
p.r = [1 0];
p.g = [1 0];
p.b = [1 0];
p.str = {'white' 'black'};

p1 = [p.r(1) p.g(1) p.b(1)];

The advantage of this is that the structure is simply an array of pointer (r,g,b,str are stored separately in memory). Compare this to the previous approach, where we have an array of structure, and each structure has pointers to its field (there's quite a memory overhead):

>> s1 = repmat( struct('r',0, 'g',0, 'b',0), 1, 1000);
>> s2 = struct('r',zeros(1,1000), 'g',zeros(1,1000), 'b',zeros(1,1000));
>> whos
  Name      Size               Bytes  Class     Attributes

  s1        1x1000            204192  struct              
  s2        1x1                24372  struct              

On the other hand, since each of the fields of a structure is stored as an array of its own, it is up to you to enforce the fact that they have to match in length.

Some others posts if you want to read more about this:


Need Your Help

Save continuous RTSP stream to 5-10 minute long mp4 files

ffmpeg streaming mp4 rtsp vlc

How can I keep the flow (protocol rtsp, codec h264) in file (container mp4)? That is, on inputting an endless stream (with CCTV camera), and the output files in mp4 format size of 5-10 minutes of

Why use an initialization method instead of a constructor?

c++ constructor initialization

I just got into a new company and much of the code base uses initialization methods instead of constructors.