Simulink Subsystem False Results

3 次查看(过去 30 天)
The script isnt the project but rather a simple code for the sake of the doubt. This Script should be used to design a Mask that changes the number of blocks (Gain blocks) depending on the input value given from the user. The script should only construct an Input port (R) and multiple of Gain blocks which will all be connected automatically together. This script is inserted under "Mask Editor -> Code" as Gain(gcb)
Problem:
1- The Gain blocks are not generated, rather series of lines named Gain. (see picture)
2- How to assign gain value for all the Gain blocks (should be entered by the user in the Mask)
The Code:
function Gain(sys)
N = max([1 round(str2double(get_param(sys,'N')))]);
% clear out any old, unneeded circuitry
lines = find_system(sys,'SearchDepth','1','LookUnderMasks','On','FindAll','On','Type','Line');
delete(lines);
blks = find_system(sys,'SearchDepth','1','LookUnderMasks','On','BlockType','Gain');
delete_block(blks);
% change in position between blocks
blockIncrement = [40 0];
% calculate all of the required block positions
blockPosition = zeros(N,4);
blockPosition(1,1:2) = [305 23];
blockPosition(1,3:4) = blockPosition(1,1:2);
loopIncr = 1;
for ii = 2:loopIncr:size(blockPosition,1)
blockPosition(ii,1:2) = blockPosition(ii-1,[3 2])+blockIncrement;
blockPosition(ii,3:4) = blockPosition(ii,1:2);
end
% add the blocks
cblk = 1;
for ii = 1:loopIncr:size(blockPosition,1)
h = add_block('simulink/Math Operations/Gain',[sys '/Gain' num2str(cblk)],'Position',blockPosition(ii,:));
set(h,'NamePlacement','Alternate');
set_param(h,'K','N');
cblk = cblk+1;
end
% connect Connectors to Cable
add_line(sys,'R/1','Gain1/1','autorouting','on');
% connect Cable segments together
for ii = 2:loopIncr:size(blockPosition,1)
add_line(sys,['Gain' num2str(ii-1) '/1'],['Gain' num2str(ii) '/1'],'autorouting','on');
end
end

采纳的回答

Daniel
Daniel 2023-3-29
It's creating gain blocks for you. But they all have a size of 0 wide/0 tall, so they're invisible. See how Position is defined in https://www.mathworks.com/help/releases/R2023a/simulink/slref/common-block-parameters.html.
You should be able to pass the gain in as a name/value pair, same as position. So when you call add_block, add a parameter pair 'Gain',gainValue. Note that gainValue should not be a number: it has to be a string which evaluates to a number. In the mask definition, you might want to disable evaluation on the value the user provides for gain. That should preserve it as a string, I think.
  1 个评论
Ahmed Ellithy
Ahmed Ellithy 2023-3-30
编辑:Ahmed Ellithy 2023-3-30
Took a while to implement what you said, but at the end you were right! The blocks were there but, as said, so small to be visible. It was solved by adding an extra argument to blockPosition(ii,3:4).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Author Block Masks 的更多信息

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by