我想做类似下面的代码,其中所有 3 个属性都是静态已知的大小(1,10)
,而不必显式地重新编写10
at 属性声明。
classdef Example
properties(Constant)
length_of_vector = 10;
end
properties
x_data(1, Example.length_of_vector);
y_data(1, Example.length_of_vector);
z_data(1, Example.length_of_vector);
end
end
这种语法无效,有没有办法在不重写10
所有三个地方的情况下做到这一点?我的真实用例有几个尺寸静态已知的尺寸,我真的希望能够在声明中指定它们的长度,以便维护人员知道预期的尺寸,但可以更改常量并自动更新取决于它的所有属性大小。
为了澄清我可以做一些选择:
classdef Example
properties(Constant)
length_of_vector = 10;
end
properties
% this gives the behaviour at runtime I want but don't want to have to specify the 10 literally
x_data(1, 10);
% this gives the correct initial conditions but is not quite the same thing
y_data(1, :) = zeros(1,Example.length_of_vector);
% this is what I am using now, if you change length_of_vector without modifying the
% 10 here it throws an error immediately so you get pointed to where you have to fix it
z_data(1, 10) = zeros(1,Example.length_of_vector);
end
end
这些不同的方式是,obj.x_data = pi
将大小设置为(1,10)
意味着它将 设置x_data
为 1x10 向量,其中每个元素是 pi,其中x.y_data = pi
哪个大小(1,:)
将其设置为 1x1 pi,这意味着期望输入具有完全相同大小的函数中断(并且将数字从字面上写入大小比重构初始化代码要少痛苦,后者确实可以obj.z_data = 50;
在给定高度启动模拟。)