Matlab - Function taking no arguments but not stat

2019-09-21 17:18发布

This question already has an answer here:

I am trying to implement the following:

classdef asset
    properties
        name
        values
    end    

    methods

        function AS = asset(name, values)
            AS.name = name;
            AS.values = values;
        end

        function out = somefunction1
            ret = somefunction2(asset.values);
            out = mean(ret);
            return
        end

        function rets = somefunction2(vals)
            n = length(vals);
            rets = zeros(1,n-1);
            for i=1:(n-1)
                rets(i) = vals(i)/vals(i+1);
            end
            return
        end
    end
end

But I am getting the error that somefunction1 should be static. But if it's static then it can't access the properties anymore. How would I resolve this issue?

1条回答
啃猪蹄的小仙女
2楼-- · 2019-09-21 17:47

In Matlab oop the first function parameter is the object.

    function out = somefunction1(obj)
        ret = somefunction2(obj.values);
        out = mean(ret);
        return
    end

Same for all other functions. This is a implicit parameter, you call the function x.somefunction()

查看更多
登录 后发表回答