循环改变方块位置
Loop to change block position
我有一个 Matlab 脚本,它为我在文本文件中找到的每个元素创建一个模型块。
问题是所有模型都是在 window 中相互创建的。所以我正在尝试制作一个循环:
for each element in text file
I add a Model block
I place right to the previous one
end
所以它看起来像这样:
正如你在左边看到的,所有的模型都在彼此身上,我想像右边的那样放置它们。
我试过这个:
m = mdlrefCountBlocks(diagrammeName)+500;
add_block('simulink/Ports & Subsystems/Model',[diagrammeName '/' component_NameValue]);
set_param(sprintf('%s/%s',diagrammeName,component_NameValue), 'ModelFile',component_NameValue);
size_blk = get_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position');
X = size_blk(1,1);
Y = size_blk(1,2);
Width = size_blk(1,3);
Height = size_blk(1,4);
set_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position',[X+m Y X+Width Y+Height]);
在循环内部但是 returns 一个错误 Invalid definition of rectangle. Width and height should be positive.
感谢您的帮助!
一个方块的位置属性实际上并不包含它的宽度和高度,而是canvas上角的位置(见Common Block Properties):
vector of coordinates, in pixels: [left top right bottom]
The origin is the upper-left corner of the Simulink Editor canvas before any canvas resizing. Supported coordinates are between -1073740824 and 1073740823, inclusive. Positive values are to the right of and down from the origin. Negative values are to the left of and up from the origin.
因此将您的代码更改为例如:
size_blk = get_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position');
set_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position', size_blk + [m 0 0 0]);
我有一个 Matlab 脚本,它为我在文本文件中找到的每个元素创建一个模型块。
问题是所有模型都是在 window 中相互创建的。所以我正在尝试制作一个循环:
for each element in text file
I add a Model block
I place right to the previous one
end
所以它看起来像这样:
正如你在左边看到的,所有的模型都在彼此身上,我想像右边的那样放置它们。
我试过这个:
m = mdlrefCountBlocks(diagrammeName)+500;
add_block('simulink/Ports & Subsystems/Model',[diagrammeName '/' component_NameValue]);
set_param(sprintf('%s/%s',diagrammeName,component_NameValue), 'ModelFile',component_NameValue);
size_blk = get_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position');
X = size_blk(1,1);
Y = size_blk(1,2);
Width = size_blk(1,3);
Height = size_blk(1,4);
set_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position',[X+m Y X+Width Y+Height]);
在循环内部但是 returns 一个错误 Invalid definition of rectangle. Width and height should be positive.
感谢您的帮助!
一个方块的位置属性实际上并不包含它的宽度和高度,而是canvas上角的位置(见Common Block Properties):
vector of coordinates, in pixels:
[left top right bottom]
The origin is the upper-left corner of the Simulink Editor canvas before any canvas resizing. Supported coordinates are between -1073740824 and 1073740823, inclusive. Positive values are to the right of and down from the origin. Negative values are to the left of and up from the origin.
因此将您的代码更改为例如:
size_blk = get_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position');
set_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position', size_blk + [m 0 0 0]);