I have an "DataAnalyzer" classdef that has a method that slowly computes exact solutions to certain queries.
classdef DataAnalyzer < HandleCompatibleSuperClass methods function obj = DataAnalyzer(dat,varargin) obj = obj@HandleCompatibleSuperClass(varargin{:}) % lengthy constructor using dat with immutable properties
end y_results = analyze(obj,x_queries) % slow analysis method
% lots of other user methods to analyze data
endend
For debugging, we can get good enough results with a DataInterpolator, that provides all the same methods EXCEPT redefines the analyze method.
classdef DataInterpolator < DataAnalyzer properties(SetAccess=immutable) DomainX RangeY end methods function obj = DataInterpolator(x_domain,dat,varargin) obj = obj@DataAnalyzer(dat,varargin{:}); obj.DomainX = x_domain; obj.RangeY = analyze@DataAnalyzer(obj,x_domain); % ^ Can't do this!
end function y_results = analyze(obj,x_queries) y_results = interp1(obj.DomainX,obj.RangeY,x_queries,'linear'); end % all those other useful methods to analyze data
endend
From https://www.mathworks.com/help/matlab/matlab_oop/calling-superclass-methods-on-subclass-objects.html
Subclass methods can call superclass methods if both methods have the same name.
I realize that I could do something like this…
classdef DataInterpolator < DataAnalyzer properties(SetAccess=immutable) DomainX RangeY end methods function obj = DataInterpolator(x_domain,dat,varargin) obj = obj@DataAnalyzer(dat,varargin{:}); % Create a temp object, but time is a penalty.
temp_obj = DataAnalyzer(dat,varargin{:}); obj.DomainX = x_domain; obj.DomainY = analyze(temp_obj,x_domain); end function y_results = analyze(obj,x_queries) y_results = interp1(obj.DomainX,obj.RangeY,x_queries,'linear'); end % all those other useful methods to analyze data endend
But that feels like a really ugly hack, even for debug. Worse, I lose the performance benefits of constructing obj essentially twice.
Is there a smarter way to accomplish what I'd like to do here?
Best Answer