フィルターのクリア

Simulink Subsystem False Results

1 回表示 (過去 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 Center および File ExchangeAuthor Block Masks についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by