Best way to call embedded C library function from Simulink model (new to Simulink)

I've read through a lot of the Help contents regarding interfacing with external code (S-functions, legacy code tool, Matlab coder.ceval function in a Matlab function block, etc) and cannot seem to find what I'm looking for. Any help would be appreciated.
Here's what I want to do...
1) Generated embedded code to call a prebuilt C embedded library function. I don't have the C source code. 2) For simulation purposes, I would like to implement this function in Simulink.
Something like...
if (simulation is active) // execute Simulink block else if (code generation active) // call C function from embedded libary else // should not occur endif
I'm sure there's some well established way of doing this, it just isn't obvious to me from the help menus.
Thanks for any help.

Answers (1)

I don't know if this is the "best" way to achieve what you need, but one way I can think of is to use the Environment Controller block - to the "Sim" input, connect your Simulink block based implementation, to the "Coder" input, connect an Embedded MATLAB containing a call that looks like this:
function y = mylibcall(u)
%#codegen
y = 0;
if ~isempty(coder.target)
y = coder.ceval('sin', u);
end
EDIT:
It just occurred to me that Variant Subsystems might be the most elegant solution here. You just need to change a workspace variable to flag which subsystem you want to use before simulation or code generation.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Tags

Asked:

on 14 Dec 2011

Community Treasure Hunt

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

Start Hunting!