Reputation: 1260
I'm currently writing a program that basically logs data from multiple peripherals. Since I'm logging the same data from each peripheral, I saw this as a good opportunity to encapsulate this data in a structure. I originally envisioned doing this with a "C" style structure, but when I dug into MATLAB's documentation, I realized the syntax is quite different.
To be more specific, I need - in MATLAB's conventions - a 1-by-6 structure. If I were using C, I would simply define a structure prototype, and create as many instances of it as I needed. Ideally, this allows me to cleanly organize my code.
However, MATLAB doesn't seem to provide this capability. For instance, this is one way I saw how to do what I want:
patient.name = 'John Doe';
patient.billing = 1;
%Create a second "instance"
patient.name(2) = 'Someone else';
patient.billing(2) = 2;
The method above does work, being that I can add as many instances as I want. Still, I'm wondering if I could simply define a generic structure with the fields I already need? If I can, this would allow me to better distinguish between the different peripherals while maintaining cleaner and easier to follow code.
Any constructive input is appreciated.
Upvotes: 2
Views: 412
Reputation: 33864
You can make structs in a similar way to c
if you would like.
patient.name(2) = 'Someone else';
patient.billing(2) = 2;
This is simply making the elements of a struct into arrays. If you want to make an array of structs you would do this:
patient(2).name = 'Someone else';
patient(2).billing = 2;
If you want to make a struct with fields that you need you can do this:
function outstruct = createstruct(name, billing history, age)
outstruct = struct('name', name, 'billing', billing, 'hist ...
end
Although it would be just as easy to add that to the body of the code without the function.
Upvotes: 4