MATLAB: Do I get an error using global variables in the MATLAB Function block

functionglobalMATLABsimulinkvariable

Why do I get an error saying Global declaration not resolved to Data Store Memory using global variables in my MATLAB Function block?"

Best Answer

The Diagnostic Viewer may say something like "Global declaration not resolved to a Data Store Memory block registered via the Ports and Data Manager."

In MATLAB Functions in Simulink, global declarations are not mapped to the MATLAB global workspace. To use global data in your MATLAB Function block, or in any code that this block calls, you must:

(a) Declare a global variable in your MATLAB Function block, or in any code that is called by the MATLAB Function block.

(b) Register a Data Store Memory block or Simulink.Signal object that has the same name as the global variable with the MATLAB Function block.

For reference see: https://www.mathworks.com/help/releases/R2018b/simulink/ug/using-global-data-with-the-matlab-function-block.html#bshotv6-1

If using the Data Store Memory block approach a few things to note are:

(1) In the Ports and Data Manager you may need to manually create the data entry, and change the name to match the Data Store Memory block, and set the scope to Data Store Memory.

(2) You will need to specify Dimensions, Signal type (real/complex), and Data type (e.g. double) for the variable in the corresponding Data Store Memory block.

(3) The initial value will be determined by the Initial value setting given in the Data Store Memory block. Variables of the same name in for example the Base Workspace are ignored.