To assist a person, I lastly took the time to demystify that in my head and determined to share what I realized with you right this moment.
In Simulink, the use instances requiring operate handles that I run essentially the most regularly into are:
In right this moment’s submit, I’ll use a post-simulation callback for sim for example.
The Instance
mdl = ‘massSpringDamper’;
in = Simulink.SimulationInput(mdl);
out.yout
A Easy Submit Simulation Callback
In its easiest kind, configuring a post-simulation callback is simple. Here’s a screenshot from the documentation:
The enter func is a deal with to a operate that receives the unique SimulationOutput object as enter, and returns a construction with the fields to be included in a brand new SimulationOutput object. For instance, I outline this operate:
operate newout = myPostSim(out)
mySignal = out.yout.get(‘x’).Values;
newout.imply = imply(mySignal);
newout.max = max(mySignal);
newout.min = min(mySignal);
finish
Then to specify that this operate must execute after the simulation, I cross to setPostSimFcn a deal with to myPostSim utilizing the “@” character:
mdl = ‘massSpringDamper’;
in = Simulink.SimulationInput(mdl);
in = in.setPostSimFcn(@myPostSim);
out = sim(in)
One factor to notice, the SimulationOutput object returned on this case doesn’t include the unique sign yout, it solely accommodates the brand new fields created within the post-simulation operate, together with the SimulationMedata and the ErrorMessage fields. That is particularly helpful in case your simulation is producing quite a lot of information, however you’re solely all in favour of post-processed statistics, like on this case the imply, min and max.
If you wish to hold the unique information, you possibly can merely re-assign it to the output. For the above instance, this appears like:
operate newout = myPostSim2(out)
newOut = out;
mySignal = out.yout.get(‘x’).Values;
newout.imply = imply(mySignal);
newout.max = max(mySignal);
newout.min = min(mySignal);
finish
When simulating, the SimulationOutput object accommodates the unique yout and the newly computed values
mdl = ‘massSpringDamper’;
in = Simulink.SimulationInput(mdl);
in = in.setPostSimFcn(@myPostSim2);
out = sim(in)
Further Enter Arguments
When scrolling via the documentation for setPostSimFcn, we additionally see this syntax:
operate newout = myPostSim3(out,bias,acquire)
mySignal = out.yout.get(‘x’).Values;
newout.meanWithBias = acquire* (imply(mySignal) + bias);
finish
With these further inputs, you first create an nameless operate that may obtain the Simulation Output object out as enter, and cross it to myPostSim3 together with values for bias and acquire:
mdl = ‘massSpringDamper’;
myAnonymousFcn = @(out) myPostSim3(out,biasValue,gainValue);
in = Simulink.SimulationInput(mdl);
in = in.setPostSimFcn(myAnonymousFcn);
out = sim(in)
Functionally, this one line of code defining myAnonymousFcn is equal to create a wrapper operate round myPostSim3 that might appear to be this:
operate newout = myNotAnonymousFcn(out)
biasValue = 5;
gainValue = 10;
newout = myPostSim3(out,biasValue,gainValue)
finish
Right here is a further picture that I hope will assist differentiate the argument for which the worth will probably be handed to the operate when will probably be known as on the finish of the simulation (Specified utilizing @(argumentName) in entrance of the operate signature), versus the arguments for which the worth is handed instantly:
Specifying a technique of an App or MATLAB Class as Callback Operate
Here’s what my easy class does:
- Within the constructor, I retailer the title of the mannequin to be simulated and use rng to initialize the random quantity generator
- Within the runSim technique, I outline a random worth x0 that will probably be used as the identical preliminary place for all simulations
- I outline completely different random values v0 for use in every simulation
- I outline the category technique thePostSimFcn as post-simulation callback and I cross to it the values of x0 and v0 utilizing further arguments
- Within the thePostSimFcn, I retailer x0 and v0 within the simulation output object for future use
classdef myClass
properties
mdl % the title of the mannequin to simulate
finish
strategies
operate obj = myClass(mdl)
rng(‘shuffle’);
obj.mdl = mdl;
finish
operate out = runSim(obj,N)
x0 = rand; % Initialize all runs with identical place
in(1:N) = Simulink.SimulationInput(obj.mdl);
for i = 1:N
v0 = 10*rand; % Initialize runs with completely different velcity
in(i) = in(1).setVariable(‘x0’,x0);
in(i) = in(1).setVariable(‘v0’,v0);
in(i) = in(i).setPostSimFcn(@(out) obj.thePostSimFcn(out,x0,v0));
finish
out = sim(in,‘ShowProgress’,‘off’);
finish
operate newOut = thePostSimFcn(obj,out,x0,v0)
newOut = out;
% Retailer the ICs within the output for the document
newOut.x0 = x0;
newOut.v0 = v0;
finish
finish
finish
Issues to notice listed below are:
- The primary argument of the post-simulation operate thePostSimFcn is obj, the article itself.
- When configuring the post-simulation callback, I discuss with it as obj.thePostSimFcn as a result of it’s a technique of this class.
I can then use my easy class with this code:
myObj = myClass(‘massSpringDamper’);
plot(out(i).yout.get(‘x’).Values);
plot(out(i).yout.get(‘v’).Values);
legendTxt{i} = [‘Run ‘ num2str(i) ‘ – x0=’ num2str(out(i).x0) ‘ – v0=’ num2str(out(i).v0)];
subplot(2,1,1); legend(legendTxt)
Now it is your flip
Do you will have different use instances for operate handles and nameless capabilities in a Simulink context? Tell us within the feedback beneath.