MATLAB Answers

Bert
0

Structure Array example as OOP?

Asked by 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.

  0 Comments

Sign in to comment.

1 Answer

Answer by Daniel Shub
on 5 Oct 2011
 Accepted Answer

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

I have edited my answer, so I can include markup. Note that it is basically your comment, but I have renamed the property testClass to be test. Then I initialize the test property to be equal to testClass. You could also do this in the constructor. At this point you just need to read a little about OOP in MATLAB.
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
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.