I am working with somebody else\'s code in MATLAB and it looks like he is creating structures, just by using field names without declaring them at all. Is that how it works in M
You need to distinguish between structs which are just a convenient way of storing data (functionality covered by suever's answer) and instances of classes. A struct is also an instance of a class, but all properties are dynamic properties by design and you don't need to worry about it. That is not always the case.
For example, if you'd create a gui from scratch with a lot a gui elements within a figure, you need to pass around a lot of properties and values in-between the gui elements. On thing all elements have in common is the figure they are placed in. It's handle, the handle of the current figure, an instance of the figure class, can be easily obtained by gcf
in every callback function of the gui. So it would be convenient to use this handle to pass all information around within the gui.
But you can't just do:
h = figure(1);
h.myData = 42;
because the figure class does not provide the dynamic property myData
- you need to define it:
h = figure(1);
addprop(h,'myData');
h.myData = 42;
I hope the difference is clear now.