Structure Array example as OOP?

16 views (last 30 days)
Bert
Bert on 5 Oct 2011
Hey all,
The structure array as described in the documentation is pretty much exactly what I was looking for to implement in my project.
However, I am trying to approach this in an OOP-way. So far I came up with defining a patient as a class, with all the fields as properties. So far so good, but I can't quite figure out how to combine all these patients in a convenient fashion like it is done in a structure array.
So now I am left wondering whether I am overlooking an obvious feature of OOP, or that I am asking the wrong question i.e. seeing everything as a nail (with OOP as a hammer...)
Any suggestions are much appreciated.

Accepted Answer

Daniel Shub
Daniel Shub on 5 Oct 2011
I think you are overlooking a feature of OOP in MATLAB. You should be able to construct an array of your patient class. You haven't posted enough to figure out what you need to do ...
Maybe this will help you see how to use arrays of objects:
Note some of this is stolen from Bert's comments, but the lack of markup in the comments makes it difficult ...
classdef testClass
properties
result = [];
end
methods
function obj = testClass
end
end
end
classdef patientClass
properties
name = '';
test = testClass;
end
methods
function obj = patientClass
end
end
end
patient = patientclass;
patient(1).name = 'John Doe'
patient(1).test(1).result(1) = 3
patient(1).test(1).result(2) = 5
patient(1).test(2).result(1) = 2
patient(2).name = 'Jane Doe'
patient(2).test(1).result(1) = 7
  6 Comments
Bert
Bert on 5 Oct 2011
Yes! Daniel, that is exactly what I was looking for! The last stap in hinsight was pretty obvious but I completely overlooked it.
Thank you for your time and effort, it is greatly appreciated!
Bert
Bert on 5 Oct 2011
By the way, I will add aggregation and composition as tags, since I had such difficulty finding this behaviour defined as such in the documentation.
I hope it will be of any help for others to come!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!