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
답변 (2개)
Kaustubha Govind
2013년 5월 16일
0 개 추천
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
Jan Kappen
2013년 5월 16일
Kaustubha Govind
2013년 5월 17일
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
2013년 5월 17일
편집: John Kelly
2015년 2월 12일
Jan Kappen
2013년 5월 18일
편집: Jan Kappen
2013년 5월 18일
Kaustubha Govind
2013년 5월 20일
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
2013년 5월 17일
0 개 추천
Just to be clear, as of R2013a, anonymous function handles are not yet supported for code generation.
카테고리
도움말 센터 및 File Exchange에서 Texas Instruments C2000 Processors에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!