Passing data from Mask to Block

2 visualizaciones (últimos 30 días)
Oleksiy Kashuba
Oleksiy Kashuba el 19 de Mayo de 2021
Respondida: Tejas el 26 de Abr. de 2024
I have a custom block 'MyBlock' in my custom Simulink library. Basically it's clock and a Matlab function block 'MyFunction' afterwarts.
The mask of 'MyBlock' has a spinbox parameter 'nValue'. Basing on 'nValue' I compute a matrix 'myMatrix', and this is a heavy computation but it can be put in Parameter Callback, since 'myMatrix' changes only if 'nValue' is changed. The block 'MyFunction' can performe the calculation using 'myMatrix' fast and easy.
The question is: how can I pass myMatrix to the 'MyFunction'? It is not a mask parameter, so I can't get assess to it...

Respuestas (1)

Tejas
Tejas el 26 de Abr. de 2024
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:
Hope it helps!

Categorías

Más información sobre Author Block Masks en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by