필터 지우기
필터 지우기

Interpreter fails to resolve subscripted reference from within class

조회 수: 1 (최근 30일)
M Wells
M Wells 2020년 3월 19일
답변: M Wells 2020년 3월 20일
We have a class that interacts with MEX code to compute values. Each class has an id and represents a value which is computed on demand. Sometimes we want to index into these computed values and to do this we extend our main class to overload subsref in a different way. The precise details are irrelevant but I recently came accross some unexpected behaviour that I can't explain. Below is the class structure:
%% An Abstract handle superclass
classdef Super < handle
properties
end
methods
end
end
%% Our concrete class
classdef Class < Super
properties
end
methods
function [varargout] = subsref(a, s)
% Do some stuff
[varargout{1:nargout}] = builtin('subsref', a, s);
end
function s = subscriptable(obj)
s = SubscriptableClass(obj);
end
function out = doSomething(obj)
subscriptable = obj.subscriptable();
% out = subscriptable.ref; % Fails
% Unrecognized method, property, or field 'ref' for class 'SubscriptableClass'.
s = struct('type', '.', 'subs', 'ref');
out = subsref(subscriptable, s); % Works
end
end
end
%% Our subscriptable subclass
classdef SubscriptableClass < Class
properties
Subscripts
end
methods
function obj = SubscriptableClass(in)
obj = obj@Class();
obj.Subscripts = struct('ref', 10);
end
function out = subsref(a, s)
% Return what's in Subscripts property
if s.type == '.'
out = a.Subscripts.(s.subs);
end
end
end
end
The subscriptable method of Class returns an object of the SubscriptableClass subclass:
obj = Class;
subsObj = obj.subscriptable();
class(subsObj) % 'SubscriptableClass'
subsObj.ref % 10
The above works as expected, however when we index into the subscriptable object from within a Class method, we get an error (see commented out part of doSomething method:
c = Class;
out = c.doSomething();
Error: Unrecognized method, property, or field 'ref' for class 'SubscriptableClass'.
Setting breakpoints shows that neither the subsref method of Class nor SubscriptableClass is called prior to this error (the method Class/subsref is of course called exactly once to resolve c.doSomething). It seems as though the MATLAB interpreter is unable to resolve the syntax from within the method of Class. Logically it should try to call subsref on the subclass, then work its way up the hierarchy before calling the builtin. This doesn't appear to happen here.
Note that if the builtin is called on the subclass from within the superclass method, it resolves properly (see the un-commented parts of doSomething). I'm interested to know what's going on here and whether this is a bug or caused by some logical ambiguity in the class structure. I'm using 2019b but also checked this on 2019a.
tl;dr - subsref on an object behaves differently from within an object's superclass; calling the builtin resolves correctly in all contexts.

채택된 답변

M Wells
M Wells 2020년 3월 20일
This question has been answered before (see the first in the MATLAB Answers below). By design, the builtin is always called from within class methods to avoid infinite recursion. More info: https://uk.mathworks.com/help/matlab/matlab_oop/indexed-reference-and-assignment.html#br09nsm

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by