0

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?

4
  • 1
    off topic, but consider making your class a handle-class, that way you get reference semantics and avoid the excessive copying of the possibly large rawData matrix Commented Sep 1, 2013 at 22:07
  • I would say that's very on topic, @Amro! That's very much what I was trying to figure out with, "Is there a class I can inherit from..." And indeed rawData winds up being pretty good sized, or at least there will be many of them. This object array will be created from order of 100MB of raw ASCII data. I will have to investigate this more, thank you. Commented Sep 2, 2013 at 12:05
  • truth is internally MATLAB is a bit smarter than that with regards to making copyies. For value-classes, obj2=obj1 will not immediately copy the data, but wait until you actually first modify one of the objects (lazy copy-on-write). Furthermore, each property of the object can be separately shared, so you can modify one property while the others are still shared with the original object. For handle-class obj2=obj1 of course simply creates another reference to the same underlying variable, so any changes in obj2 will also reflect in obj1 Commented Sep 2, 2013 at 12:28
  • so if you pass value-class objects as input to functions, where the objects are intended as read-only, you dont have to worry about MATLAB making unnecessary copies (as long as you dont modify the objects, unfortunately MALTAB has no const keyword to enforce this). You could confirm this by inspecting the output of memory before after creating copies of objects holding large data: undocumentedmatlab.com/blog/… Commented Sep 2, 2013 at 12:34

2 Answers 2

2

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? ;)

Sign up to request clarification or add additional context in comments.

Comments

0

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'));

1 Comment

This shouldn't be an answer, but rather an addendum to your question. Also, if my answer proved to be useful, please consider marking it as accepted by clicking the checkmark (✓) next to the voting buttons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.