Specifying Callbacks for Simulink Block Types - matlab

I'm trying to figure out how to set a callback for for every instance of a specific MATLAB object type and not a single instance. In my case the object type is either a SimuLink Block, Subsystem, or ModelReference.
After having read
http://se.mathworks.com/help/matlab/creating_plots/callback-definition.html
I tried
function openCallback(src, evt)
disp('here');
end
set(groot, 'defaultBlockOpenFcn', #openCallback);
but it fails as
Error using matlab.ui.Root/set
blockopenfcn is an invalid class name
Is this possible, somehow?

Simulink block callbacks are set using set_param function on a block handle. You would need to set it for a single instance of the block. I do not think it is possible to set it for every instance of a block. If you have a model you can find all the blocks of a type and set their callbacks in a loop.
You can see help for block callbacks at http://www.mathworks.com/help/simulink/ug/block-callbacks.html. You would call set_param as
set_param(gcb, 'OpenFcn', 'disp(''open fcn'')')
where gcb is a function that returns currently selected block in a model.

Related

Cannot Generate Code for Call to Simulink Function From Stateflow Chart

Consider a Simulink Simulation model including two model-reference blocks, one of them referenced to a model which calls the Add() function, the implementation of the Add() function is in the second model referenced by the second model-reference block.
Now imagine, in the Second model the Add() function is a Simulink Function scoped globally and inside it, there is a MATLAB Function block that calls a Helper() function, note that the Helper() function calls from the MATLAB function. The implementation of the Helper() function is also in the second model as expected.
Now when running the Simulation model the following error is observed:
Cannot Generate Code for Call to Simulink Function 'Helper()' from Stateflow Chart.
Now anyone can help to overcome this problem?
To test the above scenario just download the project from the following link, then open the Simulation.slx and hit run.

Can I invoke an S Function from within an S Function in Simulink?

I'm wondering if there is a way to invoke another s function from within an s function. I also have some data that I would like to pass from the main s function to the invoked one and would also like to return the value of the invoked s function back to the main s function. Any idea how to approach this? Thanks.

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.

Error Recalling Data in GUI For Loop

I have a GUI callback that includes a for loop but compared to running the same code in a script, the data/variable being retrieved within the loop after the loop ends is not being recognized and instead MATLAB says that the variable is undefined. The script runs fine with the same inputs. I am well aware that scripts and functions have different workspaces, but does this mean that the recent data in a function after ending a loop is forgotten?
The callback belongs to a push button while the data I am handling in that callback is symbolic.
I would like to know what is causing this problem
There is difference between functions and scripts.
functions have their local variables. These variables are defined within the scope of that function only. Inside of a function is isolated from the caller scope. The only way to exchange data is input and output parameters of the function.
When the flow of program reaches to a function, all variables are stored in stack, and the execution of function starts without any variables except the input parameter. On the exit from function, all local variable defined in the function are deleted, and the stored variable on the stack will be returned back to the scope.
To circumvent this mechanism, you can use
global variables
assignin and evalin functions
But this is not considered as a procedural programming.

Simulink block callbacks: How do I access block parameters in StartFcn?

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');