Simulink Subsystem False Results
4 views (last 30 days)
Show older comments
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1336429/image.jpeg)
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
0 Comments
Accepted Answer
Daniel
on 29 Mar 2023
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.
More Answers (0)
See Also
Categories
Find more on Author Block Masks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!