dotReference with matrix indexing and "end" keyword
Show older comments
Somewhat related to this https://www.mathworks.com/matlabcentral/answers/2102526-when-would-dotlistlength-as-part-of-matlab-mixin-indexing-redefinesdot-return-something-other-than-1 but I figured the question is different enough to split it into a separate question.
In the following example, I'm noticing something strange when addressing a matrix with "end" keyword:
classdef ScalarStructClass2 < matlab.mixin.indexing.RedefinesDot & ...
matlab.mixin.Scalar
properties (Dependent, SetAccess=private)
FieldNames
end
properties (Access=private)
AddedFields struct
end
methods
function out = get.FieldNames(obj)
out = string(fieldnames(obj.AddedFields));
end
end
methods (Access=public)
function obj = ScalarStructClass2(fieldName,fieldValue)
if nargin == 1
obj.AddedFields = fieldName;
return;
end
obj.AddedFields = struct(fieldName,fieldValue);
end
function out = getAddedFields(obj)
out = obj.AddedFields;
end
function [a, b] = getSomething(obj)
a=1;
b=2;
end
end
methods (Access=protected)
function varargout = dotReference(obj,indexOp)
fprintf(1, "At dotReference\n");
disp(indexOp)
[varargout{1:nargout}] = obj.AddedFields.(indexOp);
end
function obj = dotAssign(obj,indexOp,varargin)
[obj.AddedFields.(indexOp)] = varargin{:};
end
function n = dotListLength(obj,indexOp,indexContext)
n = listLength(obj.AddedFields,indexOp,indexContext);
fprintf(1, "Num Fields: %i\n", n);
end
end
end
myStructClass = ScalarStructClass2("Field1",75)
myStructClass.testField = randn(6,6);
"testField" is a 6 by 6 matrix. I access it with
myStructClass.testField(2:4,3:end)
This calls "dotReference" twice with different indexOp passed into it:
At dotReference
IndexingOperation with properties:
Type: Dot
Name: "testField"
At dotReference
1×2 IndexingOperation array with properties:
Type
Name (when Type is Dot)
Indices (when Type is Paren)
It seems the first dotReference call has indexOp without the parenthesis while the 2nd call has the Paren and the Indices are resolved:
K>> indexOp(2)
ans =
IndexingOperation with properties:
Type: Paren
Indices: {[2 3 4] [3 4 5 6]}
This seems to be the behavior is index with "end" is used. If "end" is not used in the index, dotReference is only called once. I suspect that this is done to resolve what "end" is in terms of numbers.
The problem for my real use case is that I'm overloading this operation to fetch the values from a server off of my machine. So querying the server can be slow, especially for large values.
However, the size of the variable is stored in Matlab. Ideally, I would like to have one call into dotReference with the "end" keyword if needed, and I can calculate the indices myself. Is something like this possible?
Accepted Answer
More Answers (0)
Categories
Find more on Customize Object Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!