Simulink中Matlab函数无法输出多维矩阵
例如,我有创建矩阵 2x2 的函数:[1 2; 3 4] 我有这样简单的函数:
function[result] = Rho(x)
// I've tried and so:
result = [1 2; 3 4];
// And so:
result(1,1) = 1;
result(1,2) = 2;
result(2,1) = 3;
result(2,2) = 4;
在Matlab窗口中我看到正确的结果:
>> Rho(1)
ans =
1 2
3 4
但在Simulink中我总是得到[1;2;3;4]。我的错误在哪里?
PS 我忘记删除函数的参数 x,因为在实际函数中我根据参数 x 制作矩阵。但它在我们的例子中不起作用
For example I have function that creates matrix 2x2 : [1 2; 3 4]
I have such simple function:
function[result] = Rho(x)
// I've tried and so:
result = [1 2; 3 4];
// And so:
result(1,1) = 1;
result(1,2) = 2;
result(2,1) = 3;
result(2,2) = 4;
In Matlab window i see right result:
>> Rho(1)
ans =
1 2
3 4
But in Simulink I always get [1;2;3;4]. Where is my mistake?
P.S. I forgot to remove argument x of function, because in real function I make matrix depending on argument x. But it doesn't play role in our example
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到的问题可能是由于 MATLAB Function 模块 的参数设置(现在称为 新版本中解释的 MATLAB Function 块)。查看该块的参数对话框:
请注意,您需要设置 输出维度为 2,并取消选中将二维结果折叠为一维复选框。如果选中此选项,则通过从左到右提取每列的值,您的 2×2 矩阵将转换为一维数组,最终为
[1 3 2 4]
在你的例子中。应用上述更改后,您所要做的就是调整 < 的大小strong>显示块,以便它显示 2 行和 2 列。
The problem you are having is likely due to the parameter settings for your MATLAB Function block (now called an Interpreted MATLAB Function block in newer versions). Take a look at the Parameters Dialog Box for that block:
Note that you will want to set the Output dimensions to 2 and uncheck the Collapse 2-D results to 1-D check box. If this is left checked, then your 2-by-2 matrix will be turned into a 1-D array by extracting values along each column from left to right, which ends up being
[1 3 2 4]
in your example.Once you apply the above changes, then all you should have to do is resize your Display block so that it shows your 2 rows and 2 columns.