Can you access a Data Store variable in a MATLAB function WITHOUT having it as a function input?

2 views (last 30 days)
I have a system in Simulink that uses a ton of global variables that I set up using Data Store Memory blocks, and with that I have subsystems that contain MATLAB functions that are called. One function, for example, increments a variable at the end of each run. I have this variable defined and used elsewhere in the simulation, and I don't want this function to have any input arguments. How can I access the variable in this way?
To visualize, I have my function called in my simulation, and inside this function I would like to manipulate IncTest WITHOUT having this funciton returning anything or taking any input arguments.
% Ideally I could do something like:
% extern int IncTest;
function TestFunction()
% Function
% .
% .
% .
IncTest = IncTest + 1; % Done at the end of function
end
When it comes to a counter like this, I know there are workarounds I can do, such as build this function using Simulink blocks instead of the MATLAB function block, or increment it when the operation is complete outside the function. The obvious solution is to just set it as an input argument, but I would rather not for specific cases that use a good handful of all the variables, and when I am trying to keep this simulation as close to my original project as possible (A project already coded and implimented). The problem is that I have other cases where these options and workarounds wouldn't apply, so I was wondering how I can get my Data Store variable into the MATLAB function like this.

Accepted Answer

Thomas MacGregor
Thomas MacGregor on 26 Oct 2021
The "See Also" Tab to the right just saved me the trouble it seems, i finally found the page that has the info
Somehow I missed this during my search, but to ansfer for anyone else who stumbles upon here later on:
% Ideally I could do something like:
% extern int IncTest;
function TestFunction()
global IncTest;
% Function
% .
% .
% .
IncTest = IncTest + 1; % Done at the end of function
end
Define IncTest as a global, and then in the Data and Ports menu set a new data variable to be named IncTest, and set the Scope to be Data Store Memory.
Funny enough, I did these two steps seperatly in previous attempts, but both together gets me exactly what I need.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!