MATLAB: In an S-Function Block, how to call functions that are available on the target device but not on the host computer

arduinocoder targethosts-functionsimulinktarget

Best Answer

In order to get this to work with "coder.ceval()", you have to use "coder.target()" to specify that the functionality that you are leveraging is from the target device. This will cause the specific code to only run on the target device (when deployed) and not when run on the host device (simulated, etc.). Please see the following example:
x = 0;
if coder.target('Rtw')
% generating code for Arduino
coder.cinclude('Arduino.h')
x = coder.ceval('my_arduino_fcn', args...);
else
% simulating in Simulnk
% if there is a way to replicate this behavior in MATLAB, put it here
end
Here, "coder.target('Rtw')" will prevent the next few lines from being executed when running on your host computer, where "Arduino.h" is not defined. For more information on "coder.target()", please refer to the following documentation page:
In this case, you will only want to generate code and not build and run the resulting executable. If you try to run the executable on your host computer, it will fail, since "Arduino.h" and its dependencies are not on the path on your host machine.