S-function interfacing problem: Storing a C-struct in a global data store?

1 view (last 30 days)
Oyun
Oyun on 7 Feb 2011
Hi all,
General info: - Matlab version: 7.10.0. R2010a (Simulink)
I have a s-function (C-code) which expects a C-struct as an input. This s-function is called several times during processing. The required C-struct is read from a file in the initiating procedure. The struct is variable in length.
And now my questions:
1. How am I supposed to store this C-struct in a global buffer?
2. And how do I feed this struct back into the s-function?
3. Do I need some kind of struct serializing/deserializing function? Or can I somehow just pass a pointer (memory location) to the s-function?
Since I'm new to Simulink, example code is very much appreciated.
Thanks in advance.

Answers (2)

MarkB
MarkB on 7 Feb 2011
Simulink itself doesn't read C header files, so you will need to make a Simulink.Bus object that describes the structure to Simulink. From there, there S-functions have a bus API for accessing the elements of the bus coming from/going to Simulink. You will need to use this API to access the bus elements and then populate an instance of the C structure that you have inside of your S-function. This can be a little bit tedious, so I would recommend creating the Simulink.Bus objects, and using the Legacy Code Tool to create an S-function that uses the bus objects as inputs and/or outputs.
The code generated by the Legacy Code Tool will give you a good starting point/example for how to access the bus object.

Oyun
Oyun on 8 Feb 2011
Hi there,
thanks for your answer. Yes, i have already used LCT to generate my S-function. But what if you are not allowed to use bus objects due to embedded issues?
Thanks, Oyun
  1 Comment
MarkB
MarkB on 8 Feb 2011
Could you clarify what you mean by "embedded issues"? In general terms, bus objects turn into structures during code-generation, and it sounds like you are already using structures. Bus objects are really the only way to accommodate C structures, so all we can really do is try to minimize the impact of whatever issues you have in mind.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!