MATLAB Answers


Indexed array access in a class method without subsref

Asked by David
on 21 Mar 2014
Latest activity Commented on by David
on 24 Mar 2014

I'm converting a class in Matlab 2008 from a group of m files in an @className folder, and have run into issues trying to take the individual methods within the subsref file and make them into their own methods for the class. Specifically I can get them to work, so long as there are no indexed array calls to these methods from subsref. I cannot get around no longer supporting the indexed array access to subsref methods, as it would require too much time to enact in other portions of the code base. Is there any way to make indexed array access work and be able to pull the methods out of subsref?


Very little of this is clear (to me at least). What do you mean by "individual methods within the subsref file"? The subsref file would have contained only 1 method, namely the subsref method. Also what do you mean by an "indexed array call to a method"?

The subsref function as it is currently written exists with a switch-case based on the argument in, and each of these cases I am attempting to make into a single method, instead of as a case within the subsref function. That is what I mean by individual methods within the subsref file.

Many of these cases are only reformatting the data or converting it into a different coordinate frame, so the data comes out as a large matrix. Often the calls to the different cases only one a particular row or column of data from the output, and this currently is all done in a one line call. Referencing the particular row(s), column(s) or individual elements of the matrix in a single line is what I was describing as the indexed array call to a method.

For example, for an object named T, with a case inside of subsref "geo" (converting from ecef coordinate frame to geodetic coordinate frame) and I only need the last row of data from object T, the call currently is the following:

T.geo( end, : )

Defining "geo" as its own method, not as a case in subsref, the above line results in an error. The current code base has many similar type calls which could be replaced with two lines instead of the above one, but this would be very time consuming and require drastically increasing the amount of time and testing needed to complete the task. (Neither of which is available).

Log in to comment.

1 Answer

Answer by Matt J
on 21 Mar 2014
Edited by Matt J
on 21 Mar 2014
 Accepted Answer

Implementing expressions of the form

T.geo( end, : )

is not possible when "geo" is a class method. Note that that is only true, however, because of the presence of colon expansions ':' and 'end' statements. If your indices will always be purely numeric, like in the following


then doing this through a class method called geo() is perfectly feasible.

Since you seem to want to use colons and "end"'s, one thing you might do is make "geo" a Dependent property (or maybe even a non-dependent one) that returns the matrix to be indexed. If you use a Dependent property, however, the class method set.geo() will have to construct the full matrix to be indexed, regardless of whether you only intend to extract a piece of it. If this is the very thing you were trying to avoid, you must use an overloaded SUBSREF, as in the original implementation.

  1 Comment

Thanks Matt, with the amount of data being stored by making all the parts of SUBSREF may cause memory issues but that much more clearly explains to me my options going forward and how I can get to where I need to.

Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!