필터 지우기
필터 지우기

How to overload a single parentheses indexing operation, not all of them?

조회 수: 21 (최근 30일)
Barry
Barry 2021년 12월 31일
편집: James Lebak 2022년 1월 3일
Beginning in MATLAB R2021b it is possible to overload parentheses, dot, and brace indexing independently. This gives the ability to overload paren-indexing while preserving the default behavior and performance of dot-indexing for the class.
But if I try to implement a single operation I get the message
Abstract classes cannot be instantiated. Class 'Vec' inherits abstract methods or properties but does
not implement them. See the list of methods and properties that 'Vec' must implement if you do not
intend the class to be abstract.
Abstract methods for class Vec:
parenListLength % defined in matlab.mixin.indexing.RedefinesParen
parenDelete % defined in matlab.mixin.indexing.RedefinesParen
parenAssign % defined in matlab.mixin.indexing.RedefinesParen
parenReference % defined in matlab.mixin.indexing.RedefinesParen
empty % defined in matlab.mixin.indexing.RedefinesParen
cat % defined in matlab.mixin.indexing.RedefinesParen
size % defined in matlab.mixin.indexing.RedefinesParen
It seems overloading all of them is rather hairy and involved. Is there a way to provide just a single method that I need?
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 12월 31일
What kind of change of behaviour could you be wanting to implement, that does not also affect things like size() or cat() behaviour ? Are you, for example, just adding statistics counting the number of negative values accessed? But only for one of the access methods ??
Barry
Barry 2021년 12월 31일
Yes, that is a good use case. Adding some statistics.
What I really want to do is too overload A(indices1,indices2).values (where values is an array property) to return a single array with all of the values from all the indices1, indices2 entries of A. Instead it returns a list which is cumbersome to work with. I realize I can overload the get() for .values for a single object A but it does not seem possible to overload (in a combined way) for the subset of entries of A.
Thanks.

댓글을 달려면 로그인하십시오.

답변 (2개)

Matt J
Matt J 2021년 12월 31일
편집: Matt J 2021년 12월 31일
Here is a way to do it by overloading subsref. You might have to do the analogous implementation of subsasgn, if assignments are needed.
methods
function varargout=subsref(objs,subs)
switch subs(1).type
case '()'
value=reshape([objs.value],size(objs));
varargout={builtin('subsref',value,subs)};
otherwise
[varargout{1:nargout}]=builtin('subsref',objs,subs);
end
end

James Lebak
James Lebak 2022년 1월 3일
편집: James Lebak 2022년 1월 3일
I would not do this by overloading indexing. As you point out, overloading indexing is difficult, and as Walter points out you should only overload indexing if you are really fundamentally changing the behavior of the object. It sounds to me like you want the objects to act like a default MATLAB object array but you want a convenient way to concatenate the properties.
You may be able to accomplish most of what you want by writing a method on your object that concatenates the values of the property into an array. For example,
function out = getValuesAsArray(obj)
out = [obj.values];
end
Then you could invoke the method on a subset of your array like so:
x = obj.getValuesAsArray(); % Concatenates all the values from the array
x = obj(1:10,:).getValuesAsArray() % Concatenates the values from the first 10 rows of every column

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by