Accessing a property or method when inheriting from RedefinesDot

2 views (last 30 days)
When inheriting from matlab.mixin.indexing.RedefinesDot, I have noticed that dot-indexed properties do not trigger the dotReference or dotAssign methods
A simple example:
classdef MyDotClass < matlab.mixin.indexing.RedefinesDot && ...
matlab.mixin.indexing.OverridesPublicDotMethodCall
properties
MyProperty = 1
end
methods (Access = protected)
function out = dotReference(this, idxOp)
out = [];
disp("Dot referencing [" + idxOp(1).Name + "]")
end
function out = dotAssign(this, idxOp)
out = [];
disp("Dot assigning [" + idxOp(1).Name + "]")
end
function n = dotListLength(this, idxOp, idxContext)
n = 1;
end
end
end
m = MyDotClass();
result = m.MyProperty; % result = 1; (And doesn't print)
result = m.RandomThings; % result = []; (And prints >> Dot Referencing [RandomThings]
Inheriting from matlab.mixin.indexing.OverridesPublicDotMethodCall solves the issue for when public methods are called, but not for properties. Also, this behaviour is different than with subsref and subsassign, which actually trigger when a property is used.
My question is: is there is a way to also capture properties in the dotAssign and dotReference?
  1 Comment
James Lebak
James Lebak on 11 Sep 2023
No. This is by design. RedefinesDot does not override properties that are accessible in the present context.

Sign in to comment.

Accepted Answer

Yash
Yash on 30 Aug 2023
You are correct in your observation that when inheriting from matlab.mixin.indexing.RedefinesDot, the dotReference and dotAssign methods are not triggered when accessing properties. This is because these methods are designed to work with indexing operations, not property access.
Property access in MATLAB is typically handled by the get and set methods. When you access a property using dot notation (e.g., obj.Property), MATLAB automatically calls the get method to retrieve the property value. Similarly, when you assign a value to a property (e.g., obj.Property = newValue), MATLAB calls the set method to perform the assignment.
If you want to capture property access and assignment, you should override the get and set methods in your class, like this:
classdef MyDotClass < matlab.mixin.indexing.RedefinesDot && ...
matlab.mixin.indexing.OverridesPublicDotMethodCall
properties
MyProperty = 1
end
methods
function value = get.MyProperty(this)
disp("Getting MyProperty");
value = this.MyProperty;
end
function this = set.MyProperty(this, value)
disp("Setting MyProperty");
this.MyProperty = value;
end
end
methods (Access = protected)
function out = dotReference(this, idxOp)
out = [];
disp("Dot referencing [" + idxOp(1).Name + "]")
end
function out = dotAssign(this, idxOp)
out = [];
disp("Dot assigning [" + idxOp(1).Name + "]")
end
function n = dotListLength(this, idxOp, idxContext)
n = 1;
end
end
end
With these modifications, you can capture property access and assignment in your class. When you access or assign the MyProperty property, the get and set methods will be called, and you can insert your custom logic there.
I hope this helps.
  1 Comment
Antonio Hortal
Antonio Hortal on 30 Aug 2023
Thanks a lot @Yash for the reply!
I'd argue that if there's a way to override method calls it (inheriting from matlab.mixin.indexing.OverridesPublicDotMethodCall), it would also make kind of sense to have a similar way to override property referencing and assignment. But I get this would be another that is already covered with the property getters and setters.
I will stick with subsref and subsasgn for now :)
Thanks again

Sign in to comment.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!