Programmers learn & share

Recently, I have started working in MATLAB and I came to know that this software has got some fantastic features to build any type of code. Still, there are specific rules that must be followed first, for the objects used as indices. Many times, we work on building an efficient code, but it does not run. After spending quite a time debugging, we come to know that the error occurred is so fundamental in a level like either about case sensitivity or we forget to use the correct syntex.

One of the basic error in MATLAB is"Function 'subsindex' is not defined for values of class 'cell'.” Indexing is a part of substructure, field indexing, and objects structure used as indices. The primary example of indexing in matrix operation is given below, For example:

Using Objects as Indices

MATLAB can use objects as indices in indexed expressions. The rules of array indexing apply — indices must be positive integers. Therefore, MATLAB must be able to derive value from the object that is a positive integer, which it uses in the indexed expression. Indexing expressions like X(A), where A is an object, cause MATLAB to call the default subsindex function unless such an expression results in a cell to an overloaded subsref or subsasgn method defined by the class of X. See Scenarios for Implementing Objects as Indices.

subsindex must return the value of the object as a zero-based integer index values in the range 0 to prod(size(X))-1).

Scenarios for Implementing Objects as Indices

If you want to enable indexing of one object by another object, such as X(A), you can implement this behavior in various ways:

Define a subsindex method in the class of A, which converts A to an integer. MATLAB calls A's subsindex method to perform default indexing operations (when the class of X does not overload the default subsref or subsasgn method).

If the class of X overloads subsref or subsasgn, these methods can explicitly call the subsindex method of A. In this case, ensure that A implements a subsindex method with appropriate error checking in your program.

If the class of X overloads subsref or subsasgn, these methods can contain code that determines an integer index value without relying on the class of A to implement a subsindex method.

Implementing subsindex

MATLAB calls the subsindex method defined for the object used as the index. For example, suppose you want to use object A to index into object B. B can be a single object or an array, depending on your objectives.

C = B(A);

A subsindex method implemented by class A might do something as simple as converting the object to double format to be used as an index, as shown in this sample code,

function ind = subsindex(obj)

ind = double(obj);


The above MATLAB code shows, how to index an object, we can implement a particular converter method that returns a numeric value representing an object based on particular values of object properties.

function ind = subsindex(obj)

ind = obj.ElementPosition;


This is another example of object indexing

posted Mar 31 in other by (100 points)   | 38 views
Nice article, Thanks for sharing.
2,218 questions
2,683 answers
241 users