0

I have the following problem.

I have a SIMULINK model and in this model I have a block: Matlab Function, the following http://it.mathworks.com/help/simulink/slref/matlabfunction.html

To my function I have in input :

  • a vector (N x 1)
  • a constant (1 X 1)

In output I would like to have a 3D Matrix, so a matrix R with dimensions (3 X 3 X N).

But I get the following error:

Data 'R' (#41) is inferred as a variable size matrix, while its specified
type is something else.
Component: MATLAB Function | Category: Coder error

Could you please help me?

The function in the block is the following :

function R = fcn(u,n_robots)

% u is the vector of the quaternions.
% If there are 2 robots involved the u will look like this:
% u=[w1 x1 y1 z1 w2 x2 y2 z2]'
%#eml
assert(n_robots<10);
% Initialization of the variables. If you remove this it won't work because
% code generation doesn't support dynamic changing size variables.
w=zeros(n_robots,1);
x=zeros(n_robots,1);
y=zeros(n_robots,1);
z=zeros(n_robots,1);
n=zeros(n_robots,1);

Rxx=zeros(n_robots,1);
Rxy=zeros(n_robots,1);
Rxz=zeros(n_robots,1);
Ryx=zeros(n_robots,1);
Ryy=zeros(n_robots,1);
Ryz=zeros(n_robots,1);
Rzx=zeros(n_robots,1);
Rzy=zeros(n_robots,1);
Rzz=zeros(n_robots,1);

R=zeros(3,3,n_robots);

for i=0:n_robots-1

    w(i+1) = u( 1+3*i );
    x(i+1) = u( 2+3*i );
    y(i+1) = u( 3+3*i );
    z(i+1) = u( 4+3*i );

    n(i+1) = sqrt(x(i+1)*x(i+1) + y(i+1)*y(i+1) + z(i+1)*z(i+1) + w(i+1)*w(i+1));

    x(i+1) = x(i+1)/ n(i+1);
    y(i+1) = y(i+1)/ n(i+1);
    z(i+1) = z(i+1)/ n(i+1);
    w(i+1) = w(i+1)/ n(i+1);

    Rxx(i+1) = 1 - 2*(y(i+1)^2 + z(i+1)^2);
    Rxy(i+1) = 2*(x(i+1)*y(i+1) - z(i+1)*w(i+1));
    Rxz(i+1) = 2*(x(i+1)*z(i+1) + y(i+1)*w(i+1));

    Ryx(i+1) = 2*(x(i+1)*y(i+1) + z(i+1)*w(i+1));
    Ryy(i+1) = 1 - 2*(x(i+1)^2 + z(i+1)^2);
    Ryz(i+1) = 2*(y(i+1)*z(i+1) - x(i+1)*w(i+1) );

    Rzx(i+1) = 2*(x(i+1)*z(i+1) - y(i+1)*w(i+1) );
    Rzy(i+1) = 2*(y(i+1)*z(i+1) + x(i+1)*w(i+1) );
    Rzz(i+1) = 1 - 2 *(x(i+1)^2 + y(i+1)^2);

    R(:,:,i+1) = [
        Rxx(i+1),    Rxy(i+1),    Rxz(i+1);
        Ryx(i+1),    Ryy(i+1),    Ryz(i+1);
        Rzx(i+1),    Rzy(i+1),    Rzz(i+1)];
end
4

2 回答 2

3

您可能需要对整个模型进行参数化,以便使用不同数量的机器人重复仿真并收集结果。

要么这样,要么找出机器人的最大数量并确保所有变量都设置为最大容量 - 用 0 或 -1 或 NaN 填充机器人未使用的空间以将其标记为未使用(任何适合您的。

于 2014-11-25T17:47:59.120 回答
0

我同意@James 给出的答案,用尺寸参数化模型,是理想的。您可以声明为 MATLAB Function Blockn_robots不可调整参数,然后在每次仿真期间该值将被视为常量。反过来,这将在R每次模拟期间固定大小。但是,可以在模拟之间更改该值以处理其他数量的机器人。

但是,为了完整起见,您可以将输出信号 , 设置R为可变大小。在 MATLAB Function Block 编辑器中,单击“编辑数据”。查找并选择 的条目R。选中“可变大小”复选框并输入适当的上限。[3,3,10]从您的代码中似乎就足够了。

有关在 MATLAB Function Block 中使用可变大小数据的更多信息,请访问:

http://www.mathworks.com/help/simulink/variable-size-data.html

于 2014-12-03T19:20:43.167 回答