Import existing .m functions in Simulink MATLAB Function blocks
20 次查看(过去 30 天)
显示 更早的评论
Hi all,
Question about using existing MATLAB functions in Simulink MATLAB Function blocks. I have a body of code (used in a robot controller) which I am porting to Simulink, for use in a Desktop Real-Time model. A really, really quick and easy way to do this would be to plug these functions into Simulink MATLAB Function blocks - but ideally, I would like to keep the functions available outside of Simulink as well. Two questions about doing this:
- I've successfully gotten something like this working by creating a MATLAB Function block, which contains a single instruction to call my existing .m function. It seems to work as expected - signals in Simulink are impacted the way I expect them to be, MATLAB calls that I know don't work in external mode (e.g. plot() calls) don't run when the Simulink diagram is run in external mode. My question: is there some reason that this is a BAD idea that I'm not aware of?
- Alternatively, is there any better way of doing what I'm describing? I have poked around at the MATLAB Function block for a while, and haven't been able to figure out where the MATLAB code that the block uses is stored. I'd also prefer not to generate my diagrams programatically - I'd really just like to have blocks in a Simulink library which correspond one-to-one to my MATLAB functions.
Thanks!
0 个评论
回答(1 个)
Walter Roberson
2016-11-24
I remember that I tracked this down not long ago, but I cannot seem to find the posting.
MATLAB Function block references are stored as a property of the block.
When .slx model format is used, that is really a .zip file containing a bunch of xml, and the source code for a given block is stored in an appropriate subdirectory named from the containing blocks, and is stored as a .m file named the same thing as the function block is named. But knowing this is probably not going to help you safely insert a particular block of code.
Possibly you could set_param() on the block to be the code itself (as a string with embedded newlines); I have not looked into that.
On the other hand I cannot think of any reason why you should not just have your function block be a shell that invokes your real code.
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 General Applications 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!