MATLAB: Efficient indexing with nested object and object arrays

efficiency field object arrayMATLABoop

I have an object structure that is intended to represent a physical system and be easy to understand and interact with. This requires that I use an object array with each object containing other objects, structs, and properties.
My structure is close to the following:
% How I would like to access nested values, where out is an array
[out] = parentObj.myObjArray.myObj.myStruct.myProperty;
% alternatively
[out] = parentObj.myObjArray(:).myObj.myStruct.myProperty;
Of course, either of these results in the following error: Expected one output from a curly brace or dot indexing expression, but there were N results.
However, the following works without issue:
% Step through each object in objArray and get propery value
for i=1:length(myObjArray)
out(i) = parentObj.myObjArray(i).myObj.myStruct.myProperty;
end
% Then do something with 'out'
...
The reason why this doesn't work for me, is that it would require writing a loop each time I want to get an array of parameters, which is relatively frequent.
What I have tried:
I have solved this by creating a parentObj.getVarArray(obj,string) and parentObj.setVarArray(obj,string) functions which use getfield() and setfield(). For example:
classdef ParentObj
methods
function varArray = getVarArray(obj, varString)
varArray = zeros(length(obj.myObjArray),1);
% Convert varString to cell array
fields = textscan(varString,'%s','Delimiter','.');
for i=1:length(obj.myObjArray)
varArray(i) = getfield(obj.myObjArray(i),fields{1}{:});
end
end
end
end
% Usage:
out = parentObj.getVarArray('myObj.myStruct.myProperty');
This works very well from a useability perspective, despite not using native syntax, but it is slow. I am performing these operations on very large data sets.
I have also tried overriding the subsasgn() and subsref() functions, but have found this difficult. I also question if these will offer a signficant performance improvement. It seems to significantly slow down performance even if I just pass them through the subsref function and use the builtin subsref.
What is the best way to maintain my object structure while greatly improving get/set performance?
  • Fully implement subsasgn and subsref? If so, how would I do this efficienctly?
  • Implement some sort of private data structure which is not visible to the user, but is much more efficient to access. Override get.Param and set.Param methods for this?

Best Answer

Implement some sort of private data structure which is not visible to the user, but is much more efficient to access.
This. If you have a large amount of data, you need to store them sequentially in memory in a single object (i.e., not split across multiple objects and structs) in order to access them quickly.
Overloading subsref/subsasgn is just a tool to control how you want the indexing syntax to look. It has nothing to do with speed.