MATLAB Answers

0

Using function handles from *.m file in simulink model

Asked by Jan Kappen on 15 May 2013
Latest activity Edited by John Kelly on 12 Feb 2015
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

Sign in to comment.

2 Answers

Answer by 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

i reread my first post, and I think it might be a bit misleading.
I want to parametrize a simulink model only with an EXTERN matlab file. So if I have a file like (in my matlab base workspace):
function makeSim()
param1 = ...
param2 = ...
sim('model')
end
funtion y = fun(x,u)
y = x.^2+sqrt(u(1))*sqrt(u(2));
end
I thought it would work (if I understood you correctly). The function fun will be used in the embedded matlab function. But this function does not work at all, since simulink doesn't find all the parameters param1, param2, etc.
Are there other ways to define functions?
Thank you very much!
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!
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.


Answer by Fred Smith
on 17 May 2013

Just to be clear, as of R2013a, anonymous function handles are not yet supported for code generation.

  1 Comment

Sign in to comment.