Since when has it been possible to dot-index the output of a class method?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Catalytic
am 18 Mai 2022
Kommentiert: Bruno Luong
am 21 Jul. 2022
Since when has it been possible to directly dot-index the output of a class method call, like this -
classdef myClass
properties
p
end
methods
function obj=myClass(val)
obj.p=val;
end
function obj=increment(obj)
obj.p=obj.p+1;
end
end
end
obj=myClass(2)
obj.increment.p
And why then, it is still not possible to do something similar with function calls -
subfunc.a
Error: File: test.m Line: 1 Column: 1
Using the dot operator to index into the output of function 'subfunc' is not
supported.
function S=subfunc()
S.a=1;
S.b=2;
end
Akzeptierte Antwort
Walter Roberson
am 19 Mai 2022
Since R2019b.
Indexing: Use dot indexing into function calls
You can now use dot indexing to index into the result of a function call. MATLAB evaluates the function and then applies the dot indexing operation to the result.
For example, this function creates a structure:
function out = createStruct(in)
out = struct("aField", in);
end
You can call this function and immediately access the structure field it creates:
createStruct(3).aField
7 Kommentare
Walter Roberson
am 21 Jul. 2022
Oh, of course, that second example thinks it is indexing into a variable named 'struct', makes more sense now.
Bruno Luong
am 21 Jul. 2022
It does not do what you want but it runs without error
figure(gcf().Number).Name = 'hello'
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!