Structure Array example as OOP?

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

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 can see how I could put classes into an array, but that's not really in the OOP-spirit, is it?
To be more specific about what I need, I'll use the nested structure example mentioned in the documentation above.
A patient has a name and a series of test results. Finally I have a trial consisting of a group of patients.
In the method of nested structures, this would look something like this:
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
In words, 2 tests have been conducted on patient John Doe, of which the first test yielded 2 results (3 and 5),
and the second test yielded 1 result (2). Jane Doe on the other hand had 1 test with 1 result (7).
From what I know of OOP in C++, I am looking for a 'has-a relationship' i.e. aggregation or composition.
This way a trial 'has-a' patient, which 'has-a' test, wich 'has-a' result.
So I think I am looking for a way to implement aggregation or composition into MATLAB.
Any suggestions whether I am on the right track and/or how to implement the method mentioned above would be much appreciated!
Arrays of objects is the MATLAB way and I think is in the OOP-spirit.
I edited my class so it has the properties you are talking about. In order for it to be a full example you would need to define a second class "test" which has the property "result". In the current implementation test can be whatever you want. You would need to define a set method to assure it is always a test object.
The patient class has a name and a test (you can define methods to assure that name is a string and that test is an object of test class).
You might also want a third class called trial, which has the property cohort or something, where cohort is an object array of the class patient.
Thank you very much for your reply, I feel we're getting close to the solution.
If I understand you correctly, I have to make two classes: a patientClass and a testClass. The patientClass would have a testClass as a property. This is exactly the thing I am trying to accomplish: implement aggregation or composition!
For now I have the following:
Three files: patientClass.m, testClass.m and main.m
%patientClass.m
classdef patientClass
properties
patientName;
testClass;
end
methods
end
end
%testClass.m
classdef testClass
properties
result;
end
methods
end
end
%main.m
close all;
clear all;
s = patientClass;
if I run main.m, I get s as a patientClass, however it contains a testClass as [0x0 double], in other words, it does not use the testClass.m to integrate this class into patientClass.
Is this functionality at all possible in MATLAB?
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.
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!
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)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!