subsref overload has fewer outputs than expected on cell attribute

조회 수: 1 (최근 30일)
Nath
Nath 2012년 12월 28일
편집: per isakson 2016년 9월 14일
I am overloading subsref in a class (reason is out topic here). Say for simplicity that my subsref should have the same behaviour as the builtin subsref in everycase. So I simply wrap a call to the builtin. But when using instance.some_cell{:}, where some_cell is a cell attribute, only the first element of the instance.some_cell is returned by my subsref.
In the following exemple, you can see a if bloc in subsref. Its only purpose is to pick the proper nargout size. The debugger shows me that varargout is of correct size (ie 3) right before my subsref returns. However, once my subsref is done, I only receive the first cell element oustide !!!
instance= exemple_subsrf_oncell({1,2,3});
instance.some_cell
instance.some_cell{:} %WEIRD returns only 1
I know numel isn't involved here, because some_cell is of a builtin type. I wonder how I should define subsref so that it returns me the proper number of outputs when using expression like instance.some_cell{:} I wish to let matlab find the proper nargout alone (the uggly if block in subsref is just a stupid attempt to solve the problem).
exemple class code
classdef exemple_subsrf_oncell < handle
properties
some_cell= {};
end
methods
function self= exemple_numel(cn)
self.some_cell= cn;
end
function varargout= subsref(self,idx)
if isequalwithequalnans(idx(end),substruct('{}',{':'}))
varargout= cell(1,length(self.some_cell));
else
varargout= cell(1,1);
end
[varargout{:}]= builtin('subsref',self,idx);
end
end
end
Thank you very much for your attention

채택된 답변

Matt J
Matt J 2012년 12월 28일
편집: Matt J 2013년 1월 4일
Sadly, it's a known and old problem with subsref overloading. You cannot overload the syntax obj.prop{:}. Since the issue has been around for at least 7 years, my guess is it's buried too deep in the MATLAB source code to ever be fixed.
You can, of course, do things like this as a workaround,
C=instance.some_cell;
C{:}
Incidentally, it is a numel issue ( EDIT: or rather an issue in the way nargout interacts with numel). For user-defined classes, MATLAB's default numel for expressions obj.prop will always return 1. Overloading numel can't fix this, because dot-indexing expressions won't invoke the overloaded NUMEL method.
  댓글 수: 5
Sean de Wolski
Sean de Wolski 2013년 1월 4일
@Nath, overload it for the class of your choice:
e.g. in an @cell folder, numel.m
Matt J
Matt J 2013년 1월 4일
편집: Matt J 2013년 1월 4일
@Nath
Isn't there a way to patch the BUILTIN numel ?
The only people who control the built-in NUMEL are TMW software engineers. As I said, if it were easy to fix, they would have done so 7 years ago. I'm actually no longer certain the bug is in NUMEL. I think it might be in NARGOUT. Specifically, NARGOUT will call NUMEL or it may not depending on the indexing syntax. It makes no difference, though. We don't have a way to overload NARGOUT either, as far as I can see.
@Sean,
Overloading NUMEL can't help because, as my example showed, there are situations when NUMEL overloads are ignored by NARGOUT.

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

추가 답변 (1개)

Daniel Shub
Daniel Shub 2013년 1월 4일
I am not following everything ... The two commands instance.some_cell and instance.some_cell{:} mean different things. See an old question of mine about cell array expansion. That said, I am not sure if you want them to return. It sounds like you want them to return the same thing. If that is the case, then add
if isequalwithequalnans(idx(end),substruct('{}',{':'}))
varargout = {varargout};
end
to your subsref. It is not obvious to me how to overload subsref so it behaves like the built-in. For example, it seems overloading subsref to give the following behavior is difficult.
instance.some_cell = {1,2,3};
instance.some_cell{:}
  댓글 수: 2
Nath
Nath 2013년 3월 8일
As you said, they are different things and I dont want them returning the same value, as to for instance cell concatenation : {instance.some_cell,1} and {instance.some_cell{:},1} are definitely not the same
Nath
Nath 2013년 3월 8일
The only (very heavy!) workaround I can think of would be to implement some cell-like class and have every cell attribute wrapped with this user cell class

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by