How do I call a custom Matlab class method in a Simulink block?
23 views (last 30 days)
Show older comments
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
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?
Accepted Answer
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.
More Answers (0)
See Also
Categories
Find more on Event Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!