0
votes

I defined this simple class in Simulink, and I want to create and share a 5x5 matrix of this class.

classdef evidential_grid
properties 
  Occ      
  Libr      
  Incert   
  Conf      
end
methods
  function obj = evidential_grid()       % Grid Constructor
         obj.Occ = 0;
         obj.Libr = 1;          
         obj.Incert = 0;
         obj.Conf = 0;
  end
end
end

In matlab, this code works to create a matrix of 5x5

 myGrid(5,5) = evidential_grid();

As we know. Simulink does not accept dynamic allocation of variables, so I should initialize it first.
For that, I created

function fcn()
%#codegen
global MySharedVariable;
coder.extrinsic('evidential_grid');
MySharedVariable(5,5) = evidential_grid();
  • a matlab function in simulink
  • a block of Data Store Memory to share a variable of "evidential_grid" type

But when I executed my simulink model I got these errors!

  • Global declaration not resolved to a Data Store Memory block registered via the Ports and Data Manager.
  • Errors occurred during parsing of MATLAB function

Please find me a solution, Thank you.

1

1 Answers

0
votes

There could be multiple issue with your code. First, in order to use global variables from MATLAB function block they need to be mapped to data store memory blocks. See the help page at https://www.mathworks.com/help/simulink/ug/using-global-data-with-the-matlab-function-block.html. You can do this by opening the "Edit data" option and adding your global variable and choosing the type as "data store memory". You can also checkout the example dsm_demo by running open_system([docroot, '/toolbox/simulink/examples/dsm_demo']).

You still cannot store output from extrinsic functions in this variable. In your case, I believe the best case would be to move your array of objects also to the extrinsic function and store it as either global or persistent data there and access the necessary properties as outputs of that function.

If you want to keep the data in simulink there are two ways to approach this. One is to make the class into a struct/bus type in Simulink and follow the data store approach. Here you would need to define your data store type as a simulink.signal object with datatype set to a bus object. If you have only 4 fields as you have shown the simpler route is to separate them into 4 different variable and have a separate data store memory for each one.