Structure Array example as OOP?

2 次查看(过去 30 天)
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.

采纳的回答

Daniel Shub
Daniel Shub 2011-10-5
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 个评论
Bert
Bert 2011-10-5
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 2011-10-5
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by