MATLAB Answers

Using C code in s-functions

13 views (last 30 days)
Ufuk  Irmak
Ufuk Irmak on 27 Feb 2013
Answered: Ahmet Tuna on 26 May 2017
Hi everyone;
I have a c code which need to be used in simulink block. The c code is working well, but I could not build a proper s-function with it. There are several errors;
1- In my c code there is a function other than main, How it should be implement in s function?
2- There is a struct which occurs a trouble also.
3- Does normal c syntax problem for matlab 'int' cause a error
4- Although I gave an initial value for my variables s function builder always wants a Ivalue error
If you help me I appreciate, I could not find a proper source yet. Thank you

Answers (3)

Kaustubha Govind
Kaustubha Govind on 5 Mar 2013
  1. It depends on where this function needs to be called from. Can you elaborate on how the function needs to be used?
  2. For structure inputs/outputs, you need to use bus signals.
  3. I don't know that you see errors using 'int' - could you please paste the offending code and the error message.
  4. Again, please paste the offending code and the error message.
Kaustubha Govind
Kaustubha Govind on 7 Mar 2013
Ufuk: At this point, your code is not even in an S-function, but is a regular C-executable with a main(). You probably need to get help from a more C/C++ forum than a MATLAB/Simulink forum. It's hard for me to tell, because it's not clear where symbols like CLOCAL, and types like 'termios' are being defined. Please follow basic C rules and make sure that you can compile your code outside of MATLAB.

Sign in to comment.

Dan on 9 Jul 2013
You declare to include some header files
#include fcntl.h #include errno.h #include signal.h #include stdlib.h #include termios.h #include unistd.h #include stdio.h #include string.h
But the Matlab/Simlink coder may fail to find these files. Consequently, when your codes refer to the variables predefined in these header, the compiler reports errors. Please see the Simulink 7 Writing S-Functions reference, you may need to define LIB_PATH, etc to have Matlab/Simulink to know where there header files are

Ahmet Tuna
Ahmet Tuna on 26 May 2017
I have the same problem. I have C code, in this code i am communicating with serial port. The C code is working. I have made the changes for mexFunction. When i gave the command mex serialPort.c it also works and compiles. But when i use the function the serial does not open. Do you have a solution?
best regards

Community Treasure Hunt

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

Start Hunting!

Translated by