Using function handles from *.m file in simulink model

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

Respuestas (2)

Kaustubha Govind
Kaustubha Govind el 16 de Mayo de 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 comentarios

Jan Kappen
Jan Kappen el 16 de Mayo de 2013
Hello, thank you very much for your answer.
I am using 2012b in the university, so it should support function handles ;)
It finally works with named functions defined in a external file. Even without coder_extrinsic. So thank you very much for the tip! Are anonymous functions generally not supported by simulink? And is there a way not to create a new function file for each function (since inline functions are not supported in a future release)?
By the way, what are the 'Fcn' and 'interpeted matlab function' blocks for, if i could use embedded matlab functions?
Again, thank you :)
I don't think you need to create a new file for the function, you can just define it as a local function (just put the definition after the main function) as the example shows. The 'Fcn' and 'Interpreted MATLAB Function' block do not support code-generation, but the (Embedded) MATLAB Function block does.
Jan Kappen
Jan Kappen el 17 de Mayo de 2013
Editada: John Kelly el 12 de Feb. de 2015
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!
Jan Kappen
Jan Kappen el 18 de Mayo de 2013
Editada: Jan Kappen el 18 de Mayo de 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!
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.

Iniciar sesión para comentar.

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

Preguntada:

el 15 de Mayo de 2013

Editada:

el 12 de Feb. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by