Effacer les filtres
Effacer les filtres

Dot notation with multiple dots in it

5 vues (au cours des 30 derniers jours)
Hugo FOTIA
Hugo FOTIA le 2 Nov 2021
Commenté : Hugo FOTIA le 4 Nov 2021
Hello, I need you help in order to understand the dot notation for invocation.
I read in the Matlab doc that
X = setColor(X,'red')
is equivalent to
X = X.setColor('red')
So if I understand well, in order to use a method from an object you can write
object.method(method parameter)
But I have a program looking like this :
x.number.text='number x';
I don’t understand what would be the equivalent in function notation when there is multiple dots in one notation.
Does it means that the object “x” has a method “number” which is also an object having a method “text” ?
So the equivalent notation would be this ?
number.text(x)= 'number x'
or
text(number(x))= 'number x'
And if it is the case, can we create objects and methods just like this ? I tought we needed to create an entire class definition with classdef, properties, methods…
I may be mistaken by my poor knowledge in oriented object programming, but anyway thanks in advance for your help !

Réponse acceptée

Steven Lord
Steven Lord le 2 Nov 2021
Dot has several different meanings in MATLAB. It can be used for method invocation as you mentioned. It can be used for property indexing or struct field indexing as well.
x.number.text='number x';
I interpret this as x being either an object with a property named number or a struct with a field named number. The object or struct stored in x.number has a property named text (if x.number is an object) or a field named text (if x.number is a struct array.) That line of code sets the property or field to the char vector 'number x'.
  1 commentaire
Hugo FOTIA
Hugo FOTIA le 4 Nov 2021
Thank you for the answer !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Software Development Tools dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by