How to detect the 'end' directive in subsref?

조회 수: 9 (최근 30일)
Alex Kashuba
Alex Kashuba 2020년 1월 30일
댓글: Alex Kashuba 2020년 1월 30일
Hi all!
I am trying to reimplement the indexing of the object. Let us take as an example this primitive class that return the substript
classdef buffer
function value = subsref(obj, S)
value = S(1).subs{1};
end
end
Let's create object
buf=buffer()
Then buf(7) returns 7. buf(3:7) returns [3 4 5 6 7]. All good so far. Then buf(:) returns char ':'. Ok, can process that. Now comes the funny part:
buf(end) returns 1
buf(end+1) returns 2
buf(1:end) returns 1
buf(2:end) returns []
Question: is there a workaround so that 'end' is not always equivalent of 1, but I would be able to add my own implementation?

채택된 답변

Steven Lord
Steven Lord 2020년 1월 30일
Overload the end method for your class as described on this documentation page.
The reason end was always taking the value 1 is probably because your object is a scalar (size [1 1]) that internally contains an array. Consider overloading size to return the size of that internal array if appropriate.
  댓글 수: 1
Alex Kashuba
Alex Kashuba 2020년 1월 30일
Yep, precisely what I needed, thanks!
P.S. No, overloading 'size' won't work for me, I'm making a fifo with random access, so internal array will partially empty, and I will have to implement the indexing separately. But overloading 'end' should do. Thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by