MATLAB: Can I use a Data Store Memory variable inside a MATLAB Function block which is inside a For Each Subsystem

simulink

I am using a global variable, which is registered as a Data Store Memory, in a MATLAB Function block and this block is inside a For Each Subsystem. During simulation, why do I get the following error?
The Stateflow chart 'For Each Subsystem/MATLAB Function' cannot be placed inside a subsystem containing a "For Each" block. This might be because of one or more of the following reasons: 1. The chart has a data with scope "Data Store Memory". 2. The chart accesses machine parented data. 3. The chart exports functions. 4. The chart is placed in a model with machine parented events. 5. The chart calls exported functions or Simulink functions from outside this chart. 6. The chart has a message

Best Answer

Using a global variable which is registered as a Data Store Memory inside a For Each Subsystem is currently a limitation which can be seen in the documentation below. This is to avoid multiple read-writes in a single time step.
The workaround is to remove the global variable from the MATLAB Function block and pass it as an input to the MATLAB Function block. You can use a Data Store Read outside the For Each Subsystem, use it as an input to the Subsystem, and then pass it to the MATLAB Function block as an input argument.