Tuning block parameters at each time step in Simscape Electrical

I am trying to find a way to change parameter values during a Simscape Electrical simulation using Matlab 2019B. I tested the method referenced below using a simple model of a battery and resistor, but the Resistor block does not seem to accept the updated parameter. Does the method below not work for Simscape Electrical blocks? Or is there a better way to do this?
StartFcn code:
set(0,'ShowHiddenHandles','on');
%Set up the arguments that will go into the gain block event callback listener
blk = 'var_resistor_test_24Jan2023/Resistor';
event = 'PostOutputs';
listener = @update_param_test;
%Create the listener
h = add_exec_event_listener(blk, event, listener);
Listener script:
function varargout = update_param_test(varargin)
%
%
%
% based on code available from Mathworks at:
%https://www.mathworks.com/matlabcentral/fileexchange/24935-tuning-block-parameters-at-every-time-step-in-a-simulation
temp=str2num(get_param('var_resistor_test_24Jan2023/Resistor','R')); % Get current Gain val
temp=temp + 1; % Increment
temp1=num2str(temp);
set_param('var_resistor_test_24Jan2023/Resistor','R',temp1);

1 Comment

It looks like the callback never happens. Is this because the resistor block doesn't allow callbacks?

Sign in to comment.

 Accepted Answer

In Simscape Electrical it will not work in the same way as in Simulink. See more About Simscape Run-Time Parameters. In Simscape Electrical you can use Fast Restart but not possible to tune parameters at each step. If what you want to achive is to tune a Resistor/Inductor/Capacitor it is best to use Variable Resistor, Variable Inductor, or Variable Capacitor.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!