Understanding how to call function inside a classdef module

21 vues (au cours des 30 derniers jours)
Matthew Eash
Matthew Eash le 15 Déc 2021
Commenté : Steven Lord le 16 Déc 2021
I would like to understand how best to call an internal method within a classdef module. I have seen two syntaxes used, and either seems to work. (I am not great with the terminology, so please correct me if it adds clarity.)
Syntax 1:
set_messages( obj, message)
Syntax 2:
obj.set_message( message )
Mathworks says either one will work, and I have no reason not to beleive that: https://www.mathworks.com/help/matlab/matlab_oop/specifying-methods-and-functions.html
Are there advantages to either syntax? Is either preferable? Is one better if the method called is from a different class? Thanks in advance.
Here is a simple example using Syntax 1.
classdef myObj < handle
properties ( SetAccess = public )
message
end
methods
function set_message( obj, message )
obj.message = message;
end
function upper_message( obj, message )
set_message( obj, upper( message ) );
end
end
end

Réponse acceptée

Matt J
Matt J le 15 Déc 2021
Neither one is preferable to the other as long as you haven't overloaded dot indexing for something else. As of 2021b, it is possible to overload how dot indexign works within the class,
If you think you might want to do that later, don't use dot indexing for method calls everywhere in your classdef.
  1 commentaire
Steven Lord
Steven Lord le 16 Déc 2021
There is another kind of specific circumstance where those two syntaxes can behave differently but it involves having multiple classes and an explicit statement of one of those classes being inferior to the other. See "Case Where Result Is Different" on this documentation page for the technical details.
But for the most part they are equivalent.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Use Prebuilt MATLAB Interface to C++ Library dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by