I am writing a program that creates Simulink Models using commands in MATLAB. Currently, any subsystem that is added will be cleared immediately, then repopulated with the blocks we want in it. My question is how could I add a the While Iterator Block back into the subsystem after it has been cleared?
Sample code:
new_system('test_while_loop')
add_block('simulink/Ports & Subsystems/While Iterator Subsystem', 'test_while_loop/Subsystem_loop')
Simuink.SubSystem.deleteContents('test_while_loop/Subsystem_loop')
add_block('simulink/Ports & Subsystems/While Iterator', 'test_while_looop/Subsystem_loop/While Iterator')
This comes back with the error There is no block named 'simulink/Ports & Subsystems/While Iterator, even though the documentation for Simulink says that this block is contained within the Ports & Subsystems library. What do I need to change to be able to add this block?
For built-in blocks you should use block type to add the block to your system. To identify the block type use
get_param(gcb, 'BlockType')
For the while iterator block this will return 'WhileIterator'. You can add this block to your system using
add_block('built-in/WhileIterator','test_while_looop/Subsystem_loop/While Iterator')
See documentation for add_block at https://www.mathworks.com/help/simulink/slref/add_block.html.
Related
I have a model in Simulink (2018a) which has more models inside. So I have:
parent.slx --> child.slx
I want child.slx to execute only once in the whole simulation, so basically its output will be constant at all times.
You should be using the Model Block to call the child model. Put this block into an Enabled Subsystem Block in the parent model and create the enable signal in the following way:
You also need to make sure that the outport blocks within the subsystem are set to hold their value when disabled.
Try putting the entire contents of child.slx within a triggered subsystem in parent.slx.
If it absolutely must be a separate file then try calling it from a Matlab User-Defined block; you may need to use eml.extrinsic sim ... I doubt that'd work though.
I am using Simulink to model a waste recycling plant out of a number of masked blocks that I created, representing sorting steps, buffers etc. Each module (that is, a masked block) has a failure probability, modeled using Discrete Events. If a failure event occurs, a triggered subsystem calls an Interpreted Matlab Function ("outside" of simulink). This function is supposed to set a parameter status of the masked block representing the module that failed as well as the upstream modules' status to 0 (because obviously, everything upstream has to stop as well or the material will just pile up).
`set_param(gcb, 'status', num2str(status));
PortConnectivity = get_param(gcb,'PortConnectivity');
sources = PortConnectivity.SrcBlock;`
Basically, this will be looped until I reach a block with no own Source Block.
This all works quite well, except for one problem: The gcb command gives me the block path to the last block I highlighted manually, and not to the block that called the Interpreted Matlab function. Is there any way to get the calling block's handle (which I would use with it's Parents parameter to access the Mask's status)? (A similar question has been asked here, with no results...)
I hope you get my problem - I'll be happy to elaborate if anything's unclear; I am not claiming to be a Simulink expert, so sorry for maybe using wrong terminology.
Ok, for everyone stumbling upon this:
For the mask that contains the caller of the Matlab Interpreted Function, in the mask editor I define a parameter 'this_block' (turn visibility off), that I initialize in the Initialisation pane using
parent = get_param(gcb,'Parent');
set_param(gcb, 'this_block','Parent')
Since this masked block (responsible for modelling the failure and its upstream communication) is itself used in another masked block also present in the library (responsible for modeling the module's behaviour), I also had to check "Allow library blocks to modify it's contents" in the mask editor Inititlization pane of the parent's mask. The parameter 'this_block' is then handed over as one of the input arguments of the called function (in my case, status_communication(u, this_block)).
I'm using Simulink to build a subsystem, which will then be built up using Simulink Coder to get an executable. I want to include this executable into my main function that is not necessarily written using Matlab. The main function is to implement a non-real time, desktop deployed feedback loop, i.e., (1) read out the output of the subsystem, (2) calculate a new input based on the reading, (3) send the new input to the subsystem.
I've managed to build-up a desktop deployed executable of the subsystem using RSim target. But in the main function (for test purpose, I am using Matlab to write the main function), the executable is one-off executed, where I can't read its output or assign new input during its running.
Thanks & Regards.
Yes
If you want to do a closed loop simulation, you most certainly need access to the simulink solver internal step() function. This can be achieved when you build a dll file and then include it via the header files etc. in your other simulation engine. But to be able to do this you need an embedded coder license. use the ert_shrlib.tlc target.
I have a virtual subsystem with a bunch of parameters. I would like to use those parameters to calculate other properties of the block. This needs to be done before the simulation starts, but after the block has been initialized.
I created a script that would do the calculations, and tried to get it to run from the StartFcn block callback. But the script cannot access the parameters (which are input by the user through the mask) in the callback. I'm guessing this is because those parameters aren't available in the Matlab workspace, only within the block.
Is there any way to access those parameters through StartFcn? Failing that, is there another way, instead of the StartFcn, through which I can perform some calculations BEFORE simulation starts?
To clarify, I cannot use the Initialization tab in the block's mask because the script requires data from other blocks too (which are available in the workspace at the start of the simulation).
Your guess is correct, block callbacks are evaluated in the base workspace, but mask parameters are part of the mask's private workspace. To access them use get_param and gcb within your callback function.
value = get_param(gcb, 'my_param_name');
Is it possible to comment out the block in Simulink like it is possible in any programming languages ? I mean, using logic, I can disable the block. but its not the best solution all the time.
I would rather disable / comment out the part of the block in my Model to test individual modules in Simulink.
I just wanted to add that in Simulnk 2012b, it is now possible to explicitly comment out blocks. Simply right-click on the block and select the option "Comment Out".
When run, the model will act as if the commented out block is not there at all. This means that input/output signals to/from this block are essentially just left open. So for example, if you commented out a gain block, the input signal would not simply pass through to the output signal.
I found a potentially useful solution in the matlab central forums.
A good way to "comment out" Simulink blocks is to use a switch block
whose control port is driven by a global constant value (parameter).
Say PARA=1 if you want to have this Simulink block in your code and
PARA=0 if you want to comment it out. Choose the threshold of your
switch w.r.t the value of PARA. The first branch of the switch should
pass the original signal to the Simulink block you want to have. The
other should end at a terminator block. In this case, no code is
executed for the Simulink block you want to comment out. And if you
use a code generator, the code generator can decide in advance,
whether you want to generate code for this block or not (depending on
the value of PARA).
Original Source
Hopefully that will work for you as well.
Matlab / Simulink r2018A:
1) Select a simulink block.
2) Edit -> Comment Out: