How to make a Simulink model constant? - constants

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.

Related

Simulink - add While Iterator Block to subsystem via MATLAB Command Line

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.

Get the handle of a block calling an Interpreted Matlab Function

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)).

Viewing SV testbench tasks,signal names in Modelsim

I want to view in Modelsim SV driver signals, tasks to debug. I don't want to add display statements everywhere to know how my test is progressing.Please let me know if there is a way we can do it. I tried adding the signals from simulation but when I restart the simulation the tool hangs
$display and (for UVM) uvm_info statements are the most common ways of debugging signals that reside inside functions and tasks. ModelSim/Questa have the ability to add signals from inside a uvm_component to a waveform, but not transient signals that exist only inside a function or task. One trick I do to view them inside the waveform or log is to create an observe signal outside the function/task that I assign the value that I want to observe whenever the task is called.

Set Filename of ToFile Block after Model has Finished

I have a series of ToFile blocks in my Simulink model that each have a unique filename (e.g. "Pulse.mat". I want to store the results of my simulation in timestamped folders based roughly on when I hit the run button / use the sim command.
My solution was to write two scripts, one called during the InitFcn callback and one for the StopFcn/CloseFcn callback (and PreSaveFcn callback).
The InitFcn callback would find all ToFile blocks and change the filename from "Pulse.mat" to something like "../runs//Pulse.mat", and the StopFcn/CloseFcn/PreSaveFcn to revert them to their original ".mat" names. This worked on my small test model, however when I attempted to integrate it into my actual model I receive the following error:
Error evaluating 'StopFcn' callback of block_diagram '<model_name>'.
Caused by:
Cannot change parameter 'Filename' of '<ToFile Block>' while simulation is running. The block was made virtual as it was optimized for simulation
From my research I discovered that the StopFcn actually executes before the simulation is "done" (for whatever reason), but why did it work in my initial test model? Do my ToFile blocks in my actual model have some property set that causes this error to occur?
The block has been virtualised in your larger model; which seems to be changing the execution point. One option might be to untick 'Block Reduction' in the 'Optimisation' pane of the model configuration parameters dialogue.
Alternatively, there might be another approach that you could try -
Setting the model properties 'StartFcn' to something like :
evalin('base','resultTimTag = datestr(clock, ''yyyymmdd_HHhMM'');')
and setting the model properties 'StopFcn' to :
targetDir = evalin('base','resultTimTag');
mkdir(targetDir);
outputs = dir('*.mat');
for i=1:length(outputs)
movefile(outputs(i).name,targetDir);
end
evalin('base','clear resultTimeTag');
It's possibly a little more brute-force than your approach, but seems to work quite nicely...

Is is possible to programmatically play a Simulink model and measure its states?

I am looking to set up a test set for an existing Simulink model. Ideally I could take full control of the model, explicitly stepping it and measuring the state of any signal on any bus in the model.
As might have been gleaned, this is the precursor of a unit testing system for the model. Being so, I can't really justify changing the model to suit the test, the test must accommodate the model as-is.
The furthest I've got so far is using load_model() to return a handle to the model. From there there seems to be a quite obscure set of functions for accessing the model. I can't see any that relate to accessing states and can't see any further commands that relate to accessing a loaded model.
The easiest way is to use the Data Import/Export function within the Simulink Preferences.
Set the checkbox States and it will store every state of your system for every time step in your workspace, also when you pause the simulation or execute it step by step.
Be aware not to set Save simulation output as single object, in this case the access would be more complicated and you need to follow the instructions here.
To add to the other answer, you probably want to check this page in the documentation: Control Simulation Using the set_param Command. Of interest are the following commands:
set_param(<model_name>, 'SimulationCommand', 'start')
set_param(<model_name>, 'SimulationCommand', 'pause')
set_param(<model_name>, 'SimulationCommand', 'WriteDataLogs')
set_param(<model_name>, 'SimulationCommand', 'continue')
Replace <model_name> by the path to your model file.