How to change Simulink model block spacing and size within Matlab script?
7 views (last 30 days)
Show older comments
I am creating a model where there are a number of input and output ports, and based on the internal values of these ports, there is a conversion block added between the in and outports. The problem I am having is that the spacing of the conversion blocks is heavily dependent on how many there are, which is determined by a nested for and if loop which checks for the number of ports, if they are empty, then a conversion block is added.
I want to create another loop, or other method, that specifies the initial position of the "top" conversion block and the spacing of the subsequent conversion blocks which is dependent on the number of conversion blocks.
This is the original code that creates and spaces the conversion blocks uniformly, irrelevant of how many there are:
% conversion block initial position and spacing
ConvPos = [375, -8, 420, 8];
ShiftOutY_Conv = [0, 35, 0, 35];
% create conversion blocks
ConvPos = ConvInitPos;
m = stuffConfig.NumOfPorts;
c = 1;
for k = 1:stuffConfig.NumOfPorts
if isempty(stuffConfig.stuffBits{m})
hConv = addConversionBlock(Path, ['conv', num2str(k)], 'fixdt(0,1,0)', ConvPos);
ConvPos = ConvPos + ShiftOutY_Conv;
% connect bus selection outport with conversion block
hSrc = hBSSubPorts.Outport(c);
hSnk = get_param(hConv, 'porthandles');
add_line(Path, hSrc, hSnk.Inport, 'autorouting', 'smart');
% connect conversion blocks with concat block inputs
add_line(Path, hSnk.Outport, hConcatPortHandle.Inport(k), 'autorouting', 'smart');
c = c + 1;
end
m = m - 1;
end
I have tried a few things, but my main problem is that I cannot figure out how the conversion blocks themselves are indexed within the script and therefore cannot write a loop or function that is specific to only certain indexes of the conversion blocks. To add, I am editing the code of someone who I no longer have access to to ask questions, and therefore have been kind of reverse engineering the concepts.
1 Comment
Piyush Aggarwal
on 20 Aug 2020
Edited: Piyush Aggarwal
on 20 Aug 2020
Hello!
Is the function 'addConversionBlock' inside the 'for' loop custom-defined? What kind of a conversion block are we talking about here? Is it a signal conversion or a data conversion or some other block? If you could provide more details on what is it you are trying to achieve & what is your adopted workflow. I cannot gain much insights from the question.
Apart from that, the bottleneck of your issue as per my understanding is that you are not able to find a way to index your model's conversion blocks in the script programmatically. So I have a simple work around for that which you can try out. This might help you to take your own script ahead and you might be able to complete the code for spacing the blocks depending on the number of inports.
% Making sure to close other loaded models
bdclose all;
% Load your Simulink model
load_system('myModel.slx');
% I am assuming that you are using the 'SignalConversion' Block type
nameConversionBlocksList = find_system('BlockType' , 'SignalConversion');
% The above command is going to store a cell array of names of all block types
% of SignalConversion in nameConversionBlocksList variable & you can later use
% {} indexing to access the names of any block
In case you are not working with Simulink library's Signal Conversion block, then you will have to change that name to the correct programmatic name of the block you are working with. There's an easy way to find that.
Just open the model, click on the block type whose programming name you wish to find. After that navigate to the MATLAB command window & enter this command.
>>get_param(gcb,'BlockType');
This command will display the programmatic name of the block.
Disclaimer: These are my personal views & does not reflect the views of MathWorks
Answers (0)
See Also
Categories
Find more on Sources 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!