matlab.lang.correction.ConvertToFunctionNotationCorrection class

Package: matlab.lang.correction

Correct error by converting to function notation

Description

Use ConvertToFunctionNotationCorrection objects in classes whose methods should not be called using dot notation. An MException object thrown by a method can use a ConvertToFunctionNotationCorrection instance to suggest converting dot notation to function notation syntax for calling the method.

Creation

Description

example

cfnc = matlab.lang.correction.ConvertToFunctionNotationCorrection(method) creates a correction that suggests converting dot notation to function notation syntax for calling the method that threw the MException object.

Input Arguments

expand all

Name of the method incorrectly called using dot notation, specified as a string scalar or character vector. method must be a valid MATLAB® identifier. A valid MATLAB identifier is a string scalar or character vector of alphanumerics (A–Z, a–z, 0–9) and underscores, where the first character is a letter and the length of the text is less than or equal to namelengthmax.

Examples

collapse all

Create a class whose methods should not be called using dot notation. Suggest the function notation syntax whenever a method of the class is invoked using dot notation.

In your current folder, create a class myClass by subclassing the handle superclass. Within a methods block, overload subsref to restrict invoking methods on myClass objects to the function notation syntax. To add a suggested syntax to the error message when a method is called using dot notation, use a ConvertToFunctionNotationCorrection instance within the subsref method.

classdef myClass < handle
    properties
        myProperty
    end
    methods (Hidden)
        function ref = subsref(obj, idx)
            firstSubs = idx(1).subs;
            if idx(1).type ~= "." || any(string(firstSubs) == properties(obj))
                % Parentheses indexing, brace indexing, or property indexing
                try
                    ref = builtin('subsref', obj, idx);
                    return
                catch me
                end
            elseif any(string(firstSubs) == methods(obj))
                % Valid method called using dot notation
                me = MException('myClass:useFunctionForm', ...
                    'Use function syntax to call the ''%s'' method.', ...
                    firstSubs);
                cfnc = matlab.lang.correction.ConvertToFunctionNotationCorrection(firstSubs);
                me = me.addCorrection(cfnc);
            else
                % Invalid method, property, or field called using dot notation
                me = MException('MATLAB:noSuchMethodOrField', ...
                    'Unrecognized method, property, or field ''%s'' for class ''%s''.', ...
                    firstSubs, class(obj));
            end
            throwAsCaller(me)
        end
    end
end

Create an instance of myClass and call the isvalid method using dot notation. isvalid is one of the methods that myClass inherits from its superclass.

myObject = myClass;
myObject.isvalid
Use function syntax to call the 'isvalid' method.

Did you mean:
>> isvalid(myObject)

Introduced in R2019b