Matlab class 动态填充一个属性

Matlab class dynamic filling of a property

我正在尝试在 Matlab class 中动态填充 属性。 我将向量传递给方法函数,然后计算各种参数。我想在 for 循环中填充属性,请参见代码示例。 OwnClassFunction 只是 class 中另一个函数的示例,但未在代码示例中实现。我怎样才能正确地做到这一点?

classdef Sicherung < handle      

    properties
        x = ([],1)
    end

    methods
        function examplefunction(object,...
                single_parameter_vector) % (n,1) n can be any size 

            for i=1:length(param_vector)

                [object.x(i,1)] = object.OwnClassFunction(single_parameter_vector(i,1));
            end
        end
    end
end

如果我尝试这样的事情

...
properties
   x = []
end
...
function ...(object,parameter)
   for i=1:length(parameter)
     [object.x(i)] = function(parameter(i));
   end

我收到错误消息 下标赋值维度不匹配

我手头没有 MATLAB 来测试,但下面的应该可以。

您的代码非常接近正确运行的方法。更改如下:

classdef Sicherung < handle      

    properties
        x = [] % initialize to empty array
    end

    methods
        function examplefunction(object,param_vector)
            if ~isvector(param_vector)
                 error('vector expected') % check input
            end
            n = numel(param_vector)
            object.x = zeros(n,1); % preallocate
            for i=1:n
                object.x(i) = object.OwnClassFunction(param_vector(i));
            end
        end
    end
end