Change value of a variable during script execution - matlab

I'm running a simulation which is controlled by Matlab. The Matlab enters a loop and controls the simulation sequentially. Is it possible to change the variable value(e.g.. a flag) by user input during loop execution so that I can control the behaviour of the simulation in real time?

If you want to change the value based on some condition, then use can use the input command.
Another option is to create a small pushbutton and then write its callback. In the callback function, you can write input command and request user input. Use the method which suits your needs.

Related

Wait for Simulink model execution

I wrote a script file in MATLAB that executes two functions as shown below:
function TempFunction()
fcn1();
fcn2();
end
After fcn1() is executed, I want the program to wait for execution of a model in Simulink, and then execute fcn2().
How can I do that?
One way to do this would be to use a Model Callback - set up a 'StopFcn' to trigger the next thing you want to run.
Alternatively, you could go into a loop polling the 'SimulationStatus' - see this page in the doc. .
you can call simulink models as functions from MATLAB as sim('Model_Name')
Read more about the options in the docs: https://uk.mathworks.com/help/simulink/ug/using-the-sim-command.html
and the sim function: https://uk.mathworks.com/help/simulink/slref/sim.html

Can I change a block parameter using another block for conditional execution?

I'm wondering whether it is possible to change a block parameter in Simulink (or the value of one saved as a variable) using a different block to enable conditional execution. What I would like to do is have a certain block parameter (in this case, Counter) run during the simulation with an initial value, and have it change to a different value if a certain condition is satisfied.
Ultimately, what I would like to get out of this is to get a Counter block to stop running upon the satisfaction of that condition.
I'm pretty new to Simulink, but I'll detail some of the stuff I've tried so far:
Dashboard switches (Slider, Knob etc.) - I know they're used to
change tunable parameters of blocks, but they cannot be linked to
other blocked and can be only be controlled manually.
Matlab Function block - didn't seem to work, I'm obviously missing something.
Is it maybe possible to disable a certain block/link when that condition is met? That would be a straight forward approach, but I'm not sure it can be implemented in Simulink. Any help would be appreciated!
So to meet your ultimate goal have you considered to place your counter in an enabled subsytem?
Whenever the requirements are met to stop the counter you simply disable the subsystem and the counter will stop.
On the output port of that enabled subsystem you will have the options to preserve the last value or reset it to certain one.

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

Avoid interruption of callback functions in Matlab GUI

I have a Matlab GUI that needs a high time to execute some callback functions. Besides, these functions include the following code:
drawnow('expose');
pause(handles.data.delay);
I want to avoid that those callback executions get interrupted in order to avoid data inconsistency if the user presses other buttons. Thus, I modify the figure settings as:
set(handles.figure, 'BusyAction','cancel', 'Interruptible','off');
However, the callbacks are still interrupted. How can I avoid it?
Note: I think that the problem is that I need to propagate the 'BusyAction' and 'Interruptible' values to all the controls in my GUI, is there any way to do it automatically? Like, for example, modifying the default value before generating the GUI.
The fastest and cleanest way to propagate any property to all UI objects is with findobj:
set(findobj('Type','uicontrol'), 'BusyAction','cancel', 'Interruptible','off');

How to vary gain value of gain block in Simulink during runtime

The Gain block and continuous block in Simulink require the user to specify a gain. This can be a workspace variable. But I want to vary this gain during runtime. I can't seem to get a solution for this. This idea is simple but I can't believe it is so difficult to implement.
I have tried using another block to write to workspace, but found out that the 'to workspace' block only writes to the workspace after the simulation ends or pauses.
I can store the variable in a data memory block, but I don't know how to specify the gain value(s) for the gain/PID block in this case.
If you have Inline Parameters turned off (it's on the Optimization page of the Configuration Set), you can just open the gain block dialog and change the value. If you want to use a workspace variable, then you can change the value of the workspace variable and do an Update Diagram (^D) while the simulation is running.
There's also a block called the Slider Gain which allows you to change the gain value using a slider UI.
May be it'd be helpful at some point: try using the MATLAB Function Block (Matlab user-defined function that can be used directly in Simulink).
As a command-line alternative, you can use the SET_PARAM function to change the Gain value of the block during model simulation.
For example, the following code would change the Gain value of a block named "My Gain" at the top level of a model called "my_model.mdl" to a value of 20:
set_param('my_model/My Gain','Gain','20');
Note, however, that only Tunable Parameters can be changed with SET_PARAM at runtime.