How do I call a custom Matlab class method in a Simulink block?

23 views (last 30 days)
I am trying to create a Simulink model that uses an instance of a custom Matlab class from the workspace to process some data in the block diagram. For example, I would like to create a "Reaction" block in my Simulink model that takes two inputs (T and F) and uses the dFdt method of a class instance from the MATLAB workspace to output dFdt(T,F) (simplified example code below).
This is different than this question in that I do not want to pass the custom object as an output of the Simulink block.
The answers to this question and this question seem to imply that this is possible using 'UserData', but it is not clear to me how.
If this is even possible, how would I do it? Thanks in advance!
Example class definition
classdef Rxn < handle
properties
k = 0;
Ea = 0;
end
methods
function r = Rxn(k,Ea)
r.k = k;
r.Ea = Ea;
end
function dFdt = dwdt(obj,F,T)
dFdt = obj.k * exp(-obj.Ea/(8.314*T))*(1-F)
end
end
end
Desired script format
materials = [Rxn(1,10000), Rxn(0.1,20000)];
for i = 1:length(materials)
R = materials(i);
% Call simulink model and use 'R.dFdt(F,T)' in Simulink block
end
  3 Comments
Ryan G
Ryan G on 15 Jul 2013
Where would the simulink model use this data? Would it be a parameter or input port? It looks like each loop iteration the simulation is run with a single parameter (result of R.dFdt). If this is the case you should be able to do something like:
1) define parameter in Simulink as a variable, say x
2) x = R.dFdt;
3) run the model.
This should work as long as you run the script as a script and not a function. Make sense?
Tyler V
Tyler V on 15 Jul 2013
@Kaustubha I want to use the value calculated by R.dFdt(F,T) as an output of a block to pass to other parts of the Simulink model. My first attempt was to use a MATLAB Function block with two inputs and one output, coded as
function dFdt = fcn(T,F)
%#codegen
dFdt = R.dFdt(T,F);
but that returns "Undefined function or variable 'R'". I have also tried adding 'R' as an input to the same block and wiring a constant with Value set to 'R', but it is not recognized as a data type because R is a custom object.
@Ryan That method works fine for the parameters I want to set, but dFdt is a function handle, not a variable. I want the value of dFdt to be calculated within the Simulink model, given the inputs T and F (also Simulink inputs). If you know of a way to pass a function handle from the workspace to a Simulink block, that could work.

Sign in to comment.

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 23 Jul 2013
Since you need the MATLAB code to compute the output as a signal, you have the right idea of using a block to call the code. However, the MATLAB Function block needs to generate C code from the MATLAB code for execution, and I'm not sure if handle classes are supported for code-generation. You might find the Interpreted MATLAB Function block (single-input, single-output only) or MATLAB S-functions to be more appropriate.
  1 Comment
Tyler V
Tyler V on 23 Jul 2013
By combining the T and F signals with 'mux' I was able to use the Interpreted MATLAB Function block with the argument
R.dFdt(u(1),u(2))
to do what I originally intended. However, it did not integrate as well as I had hoped with the Simscape ODE environment due to algebraic loops and I ended up just making some custom Simscape components that accept numeric flags and coefficients from the class and compute the ODEs internally.

Sign in to comment.

More Answers (0)

Categories

Find more on Event Functions in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!