Clear Filters
Clear Filters

S function with function handle as class member of block.

34 views (last 30 days)
younghwa park
younghwa park on 10 Sep 2024 at 11:00
Answered: Sahas on 13 Sep 2024 at 9:03
Problem statement: I want to preload my function handel form mat file in level-2 s-function. After that, I want to call each iteration.
Code sample:
function msfcn_times_two(block)
% Level-2 MATLAB file S-Function for times two demo.
% Copyright 1990-2009 The MathWorks, Inc.
setup(block);
%endfunction
function setup(block)
%% Register number of input and output ports
block.NumInputPorts = 1;
block.NumOutputPorts = 1;
%% Setup functional port properties to dynamically
%% inherited.
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).DirectFeedthrough = true;
%% Set block sample time to inherited
block.SampleTimes = [-1 0];
%% Set the block simStateCompliance to default (i.e., same as a built-in block)
block.SimStateCompliance = 'DefaultSimState';
%% Run accelerator on TLC
block.SetAccelRunOnTLC(true);
%% Register methods
block.RegBlockMethod('Outputs', @Output);
%% import function
block.lam=load("lmbd.mat")
%endfunction
function Output(block)
M = block.lam.Mfunc(0,0,0,0,0,0,0,0,0,0,0,0);
block.OutputPort(1).Data = 2*block.InputPort(1).Data;
%endfunction
How to load function handle as class member?
Thanks!

Answers (1)

Sahas
Sahas on 13 Sep 2024 at 9:03
As per my understanding, you would like to preload a function handle from a “.mat” file into a level-2 MATLAB S-function and call this function handle with each iteration.
This can be done by storing the function handle as a class member within the S-function. Utilize MATLAB’s “DWork Vector” for this by storing the function handle persistently across simulation steps.
Run the following command in the MATLAB Command Window and use the “msfcn_varpulse.m” file as a template and modify your code accordingly.
>> edit('msfcn_varpulse.m');
Refer to the MathWorks documentation below for more information on “DWork Vector” and detailed steps on how to use them for Level-2 MATLAB S-functions:
I hope this is beneficial and help you call the function handles with each simulation step.

Community Treasure Hunt

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

Start Hunting!