Using function handles from *.m file in simulink model
4 views (last 30 days)
Show older comments
Hey all,
I wonder, how i can use functions (or function handles) which are defined in an *.m script file in a simulink model. I know there are a few blocks, for example the 'interpreted matlab function' or the 'matlab function'.
The first variant works, but it does only have one input and I need multiple inputs which are vectors. The second one does not work, the function can't be found, even if i use:
coder.extrinsic('fun')
'fun' is defined in the script file, for example:
fun = @(x,u)x.^2+sqrt(u(1))*sqrt(u(2))
I also tried to mask the block where the function is needed and use it as a parameter.
But the error remains and is "error calling MATLAB funtion 'fun'".
What is the easiest way to get this working? And (maybe a future task) what is the most useful way, if i want to compile the model as c Code for the xPC or the realtime windows target?
Thank you very much, Jan
0 Comments
Answers (2)
Kaustubha Govind
on 16 May 2013
I believe the (Embedded) MATLAB Function block started supporting function handles only in R2007b, so you will only be able to use them if you are using R2007b or a newer version of MATLAB. Please see the documentation for help on how to define the function handles. All the examples there show named function handles (as opposed to the anonymous function handles that you are using), so if you are indeed using a newer release, then you may want to try naming your function as the examples in the documentation show.
5 Comments
Kaustubha Govind
on 20 May 2013
You can use simset('SrcWorkspace','current', 'DstWorkspace', 'base') to read parameters from the function workspace, but write outputs to the base workspace. I'm not aware that this is not "optimal". Could you point me to the source that you referred to?
I don't think controlling your model from an external MATLAB script has any effect on using function handles in the MATLAB Function block, so yes, you can define all your functions under the main function in the MATLAB Function Block Edtor.
Fred Smith
on 17 May 2013
Just to be clear, as of R2013a, anonymous function handles are not yet supported for code generation.
See Also
Categories
Find more on Simulink Coder 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!