简体   繁体   中英

How do I call get method in matlab?

classdef Untitled
enumeration
    M('monday','first day')
    T('tuesday','second day')
end

properties(SetAccess=private)
    name
    description
end

methods
    %constructor
    function obj = Untitled(name, description)
        obj.name = name
        obj.description = description
    end

    %getter
    function name = get.name(obj)
    end

    %getter
    function description = get.description(obj)
    end



end

end

How can I call get method in command window after I make instance of Untitled? I am new to matlab and not sure is that even possible, cause I read that getter and setter cant be called directly?

There are a couple of problems with the code you provided. First, an enumeration class is a special type of class in Matlab. You may want to read more about enumeration classes in general in Matlab, and about their restrictions .

If I assume you are not trying to create an enumeration class, and remove that part of your function, the next problem is that your getters don't do anything. You should at least do:

function name = get.name(obj)
    name = obj.name
end

However, if all you're going to do is return the value of the property, you don't even need to create a get function. Here's some code which works:

classdef Untitled

    properties(SetAccess=private)
        name
        description
    end

    methods
        %constructor
        function obj = Untitled(name, description)
            obj.name = name;
            obj.description = description;
        end

    end

end

Then you can do:

myobj = Untitled('myname','mydesc');
myobj.name
myobj.description

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM