Import existing .m functions in Simulink MATLAB Function blocks

17 views (last 30 days)
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:
  1. 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?
  2. 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!

Answers (1)

Walter Roberson
Walter Roberson on 24 Nov 2016
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 Comments
Julian Leland
Julian Leland on 26 Nov 2016
Thanks, Walter - I'll try to dig into the model and see if I can find the code in a subdirectory. If anyone else has thoughts on why this is/isn't a good way to do this, please let me know!

Sign in to comment.

Categories

Find more on General Applications 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!