Matlab - 不带参数但不是静态的函数 [英] Matlab - Function taking no arguments but not static

查看:171
本文介绍了Matlab - 不带参数但不是静态的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图执行以下操作:

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

但是我得到somefunction1应该是静态的错误。但是如果它是静态的,那么它就不能再访问这些属性了。如何解决这个问题?

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?

推荐答案

在Matlab中,oop的第一个函数参数是对象。

In Matlab oop the first function parameter is the object.

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

所有其他功能都一样。这是一个隐式参数,您可以调用函数 x.somefunction()

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

这篇关于Matlab - 不带参数但不是静态的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆