Simulink - C++ interface: How data transfer is done between a visual c++ program and the simulink engine

5 views (last 30 days)
I am having difficulty in understanding how exactly a C mex-S function accepts external data from a C program. I need to pass data (two inputs) from an MFC application to a simulink model and then retrieve the data output( two outputs) from the model and send it back to my MFC application.
My grey area is how exactly does the data transfer process take place? If I have the value I want to transfer stored in a variable in my MFC application, how do I then call my simulink model accept this variable from my MFC application as an input, process it, and then relay this ouput data back to my MFC application?
I tried using the S Function Builder in simulink but I was very confused when it came to which libraries I should include. Also, I am not seeing how using an S function transfers data to and from the application because I do not know how the simulink model knows how, when and even where to look for the input data.
I have looked through much of the documentation on S functions but none seem to specifically outline how the process is done (or maybe I am interpreting some things wrong).
Some specifics:
- The input and output data I want to transfer is of type float/double/int16 (any type that can represent decimals)
-The inputs and outputs of the model are real numbers (no imaginary term)
-The number of inputs is two(2) and the number of outputs is also two(2).
-The data transfer rate is discrete (not sure if discrete is the technically correct term in this case so I will illustrate via example) e.g. a user should be able to input the values to the MFC program and recieve the output after processing by simulink. If he decides to leave for 5 minutes and then comes back and enters a new set of inputs, the results should be again readily available (like using a calculator)
- MATLAB version: R2008b
- Visual Studio Version: 2010 Ultimate
-The run of the program is dictated by the MFC application. The application should call the simulink model to do the processing when desired
Also, is it necessary to use the Visual Studio compiler or can I simply use the native MATLAB compiler?
Please advise if it will be necessary to upgrade my MATLAB version or downgrage my Visual Studio version
Any help/example code/flow diagram of data transfer process would be appreciated. Thank you.

Accepted Answer

Ken Atwell
Ken Atwell on 13 Nov 2011
MEX S-Functions are generally envisioned as being a way to call C/C++ functions from MATLAB, and you are looking to the opposite (call MATLAB/Simulink from a C++ program). The MATLAB Engine might be better aligned with what you need to do.
  4 Comments
Kaustubha Govind
Kaustubha Govind on 14 Nov 2011
I would recommend using the MATLAB Engine API (engPutVariable and engGetVariable) to write/read variables to/from the MATLAB workspace. You can then run your Simulink model using the MATLAB "sim" command and have your model read/write from the MATLAB workspace using the "From Workspace" and "To Workspace" blocks.
Jonathan
Jonathan on 15 Nov 2011
Thank you both for the insight. I think I will explore communication via the MATLAB engine. The blog post looks useful. I think what would be best is if I convert my Simulink model to an equivalent transfer function and store it in an M file so I can communicate directly with MATLAB. I will also explore sending the data to the workspace and then to simulink.

Sign in to comment.

More Answers (1)

Mike Woodward
Mike Woodward on 26 Jun 2012
Hi,
The download shows how to build a Visual Studio project that outputs a .mexw64 file. It shows how parameters are passed into C code.
Mike

Categories

Find more on Simulink Coder 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!