Simulink Subsystem False Results

조회 수: 3 (최근 30일)
Ahmed Ellithy
Ahmed Ellithy 2023년 3월 25일
편집: Ahmed Ellithy 2023년 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