Using function handles from *.m file in simulink model

4 views (last 30 days)
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

Answers (2)

Kaustubha Govind
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
Jan Kappen
Jan Kappen on 18 May 2013
Edited: Jan Kappen on 18 May 2013
am i right, that i need
simset('SrcWorkspace','current');
when i simulate a model from within a function? Are there other ways, since it would be nice to have some outputs in my base workspace?
a small edit: I read, this "solution" isn't optimal, since simset will be removed soon. The other way is to assign the parameters in the model workspace but then the functions can't be found.
I'll ask again, just to be sure:
Is there a way to control a simulink model from an extern matlab script file or function file while using self written functions in the "embedded matlab function" block in simulink and having all the functions in ONE file (otherwise i would have to create a lot of very short function files - or can that be done automatically)?
Thank you!
Kaustubha Govind
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.

Sign in to comment.


Fred Smith
Fred Smith on 17 May 2013
Just to be clear, as of R2013a, anonymous function handles are not yet supported for code generation.

Categories

Find more on Simulink Coder 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!