Access DataStore-Variables from s-function
8 views (last 30 days)
Show older comments
Hello,
ich would like to access datastore-variables directly from s-functions. The datastore references a signal-object (with an underlying nested bus-object-structure). As these structures are represented as nested c-structures (with names equal to the bus-object-structure) it would be easy to access them directly from the c-code. I found a function called 'mexGetVariablePtr()', however I am not sure whether this the the correct way, neither I know how to use this function (if it is the correct one).
Thanks a lot for you help in advance.
Regards
Henning
2 Comments
ValenStein
on 11 Mar 2025
Hi Henning,
I am facing the same issue. Have you found a solution for it?
Best, Valentin
Answers (1)
Snehal
on 17 Feb 2025
Hello,
I understand that you are trying to access DataStore Variables from S-functions in Simulink.
Using the ‘mexGetVariablePtr()’ function for accessing Simulink datastore variables directly from S-functions is not typically recommended because this function is a part of the MATLAB API for MEX files and is used to obtain pointers to variables in the MATLAB workspace. You can use Simulink API functions for the concerned use case.
The workflow that you can follow to access DataStore Variables using the Simulink API in S-function is:
- Assuming datastore variables and corresponding bus objects are already defined, use a Data Store Memory block in Simulink to specify the storage location
- Include necessary headers such as ‘simstruc.h’ and define C structures that match the bus object hierarchy
- Use ‘DWork’ vectors to manage data and ‘ssGetDWork’ to obtain pointers to the datastore memory
- Implement ‘mdlOutputs’ to read from and write to the datastore variables
You may refer to the following documentation links for more information:
- https://www.mathworks.com/help/simulink/slref/custom-code-and-hand-coded-blocks-using-the-s-function-api.html
- https://www.mathworks.com/help/simulink/slref/datastorememory.html
- https://www.mathworks.com/help/simulink/sfg/simstruct_introduction.html#:~:text=mex%20%2Dg).-,The%20SimStruct,-The%20file%20simstruc
- https://www.mathworks.com/help/simulink/sfg/dwork-matlab.html
- https://www.mathworks.com/help/simulink/sfg/ssgetdwork.html
- https://www.mathworks.com/help/simulink/sfg/mdloutputs.html
Hope this helps.
0 Comments
See Also
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!