Hello Oleksiy,
From my understanding, you want to pass ‘myMatrix’, which is generated from a mask callback, into the ‘MATLAB function’ block within the subsystem. This can be achieved using a ‘Data Store Memory’ block.
To illustrate this, I have created a Simulink model. The model includes a spin box that allows a selection of a number, within the range of 1 to 10. This number is then used to generate a random matrix. The matrix is passed to a ‘MATLAB function’ block within the subsystem, where the sum of the matrix's elements is calculated. The result is displayed on a ‘Display’ block.
- The structure of the Simulink Model is straightforward. It consists of ‘Data Store Memory’ block named ‘MyMatrixDataStore’, a subsystem and a ‘Display’ Block.
- In edit mask section, a spin box parameter is created to select a value between 1 and 10. This value is stored under the name ‘nValue’. Additionally, a callback is associated with this parameter to create ‘myMatrix’ and store it in the base workspace.
- The ‘Data Store Memory’ block's initial value is set to ‘myMatrix’, enabling it to read ‘myMatrix’ from the base workspace and then provide it as input to the ‘MATLAB function’ block through the ‘Data Store Read’ block.
- The subsystem utilizes the ‘Data Store Read’ block to feed ‘myMatrix’ into the ‘MATLAB function’ block. It is important to name the ‘Data Store Read’ block the same as the ‘Data Store Memory’ block, to access data across workspaces.
- The ‘MATLAB function’ block is designed to take ‘myMatrix’ as input and output the sum of the elements of the matrix.
function y = MyFunction(myMatrix)
y = sum(myMatrix(:));
end
The model was tested with the spin box value set to 2, and the output is captured in a screenshot for reference.
For better understanding of this approach, please refer to below documentations:
- ‘get_param’: https://www.mathworks.com/help/releases/R2021a/simulink/slref/get_param.html .
- ‘assignin’: https://www.mathworks.com/help/releases/R2021a/matlab/ref/assignin.html .
- ‘Data Store Memory’: https://www.mathworks.com/help/releases/R2021a/simulink/slref/datastorememory.html .
- ‘Data Store Read’: https://www.mathworks.com/help/releases/R2021a/simulink/slref/datastoreread.html .
Hope it helps!