Reputation: 135
After dabbling in C# I'm now keen to use some OOP in Matlab - to date I've done none!
Let's say that I define a class for a data object with a handful of properties...
classdef TestRigData
properties
testTemperature
sampleNumber
testStrainAmplitude
sampleMaterial
rawData
end
methods
% Some constructors and data manipulation methods in here
end
end
...where 'rawData' would be a m-by-n array of actual experimental data, and the other values being doubles or strings to help identify each specific experiment.
If I have an array of TestRigData objects, what would be the best way of finding the indices of objects which meet specific criteria (e.g. testTemperature == 200)? Or getting all the unique values of a property (e.g. all the unique sample numbers in this collection).
If they were arrays of their own, (myNewArray = [3 5 7 22 60 60 5]) it would be easy enough using the find() and unique() functions. Is there a class I can inherit from here which will allow something like that to work on my object array? Or will I have to add my own find() and unique() methods to my class?
Upvotes: 0
Views: 515
Reputation: 135
The suggestion of creating this as a handle class (rather than value class) is something I need to think about more in the future... after having put together some initial code, going back and trying to change classdef TestRigData
to classdef TestRigData < handle
seems to be causing issues with the constructor.
Bit unclear of how I would go about using a hash value unique to each object... but the syntax of extracting all values to an array is ultimately what got me in the right direction.
Getting a new object array which is the subset of the original big data array conforming to a certain property value is as easy as:
newObjectArray = oldObjectArray([oldObjectArray.testTemperature]==200);
Or for just the indices...
indicesOfInterest = find([oldObjectArray.testTemperature]==200);
Or in the case of non-scalar values, e.g. string property for sample material...
indicesOfInterest = find(strcmpi({oldObjectArray.sampleMaterial},'steel'));
Upvotes: 0
Reputation: 32930
You can assign an ID value (a hash value in the general case) to TestRigData
objects and store it as a new property. You can then extract all ID values at once to a cell array, e.g {yourarray.id}
(or [yourarray.id]
if the ID values are scalars), allowing you to apply find
and unique
with ease.
Adding your own find
and unique
is definitely possible, of course, but why make life harder? ;)
Upvotes: 2