This example shows how to specify the action to take when a tunable property value changes during simulation.
The processTunedPropertiesImpl
method is useful for managing actions to prevent duplication. In many
cases, changing one of multiple interdependent properties causes an action. With the
processTunedPropertiesImpl
method, you can control when that action is taken so it is not repeated
unnecessarily.
This example of processTunedPropertiesImpl
causes the pLookupTable
to be
regenerated when either the NumNotes
or MiddleC
property changes.
methods (Access = protected) function processTunedPropertiesImpl(obj) propChange = isChangedProperty(obj,'NumNotes')||... isChangedProperty(obj,'MiddleC') if propChange obj.pLookupTable = obj.MiddleC *... (1+log(1:obj.NumNotes)/log(12)); end endend
classdef TuningFork < matlab.System % TuningFork Illustrate the processing of tuned parameters % properties MiddleC = 440 NumNotes = 12 end properties (Access = private) pLookupTable end methods (Access = protected) function resetImpl(obj) obj.MiddleC = 440; obj.pLookupTable = obj.MiddleC * ... (1+log(1:obj.NumNotes)/log(12)); end function hz = stepImpl(obj,noteShift) % A noteShift value of 1 corresponds to obj.MiddleC hz = obj.pLookupTable(noteShift); end function processTunedPropertiesImpl(obj) propChange = isChangedProperty(obj,'NumNotes')||... isChangedProperty(obj,'MiddleC') if propChange obj.pLookupTable = obj.MiddleC *... (1+log(1:obj.NumNotes)/log(12)); end end end