Effacer les filtres
Effacer les filtres

Can you create an object in Simulink that can be referenced by multiple MATLAB Fcn blocks?

27 vues (au cours des 30 derniers jours)
Roop
Roop le 10 Juil 2024 à 18:37
Réponse apportée : Paul le 11 Juil 2024 à 22:05
I've attached an image with my current Simulink set-up. I am needing to replace the Interpreted MATLAB Fcn blocks with MATLAB Fcn blocks in order to make my Simulink code generation compatible. However, I am struggling to find a way to mimick this similar OOP approach - where I am essentially creating a global object that is being referenced and manipulated by multiple blocks.
With the MATLAB Fcn block, it is only creating a local object for that specific block. It cannot be referenced and manipulated by another MATLAB Fcn block. I run into a similar issue when trying it with MATLAB System blocks. Is there a workaround for this to get this "global object" character?
  3 commentaires
Roop
Roop le 11 Juil 2024 à 15:01
I want to be able to have this variable and its value persist across multiple blocks, not just multiple calls to the same block. Is there any workaround for this?
Umar
Umar le 11 Juil 2024 à 15:50
Hi Roop,
Thanks for clarifying this issue, to maintain persistent variables across multiple Fcn blocks, you can utilize the set_param function to set the block's mask parameters. By setting the mask parameters of a block, you can effectively share variables among multiple blocks. This approach allows you to achieve persistence across various blocks in your Simulink model. Here is an example illustrating how to use set_param to maintain a persistent variable across multiple Fcn blocks:
% Set the mask parameter of the first block
set_param('model_name/Block1', 'MaskValueString', 'persistent_var=0;');
% Access the persistent variable in the second block
persistent_var = evalin('base', 'persistent_var');
By setting and accessing mask parameters using set_param and evalin, you can create a workaround to maintain persistent variables across multiple MATLAB Fcn blocks.
For more information on set_param and evalin functions, please refer to
https://www.mathworks.com/help/simulink/slref/set_param.html
https://www.mathworks.com/help/matlab/ref/evalin.html
Hope this should help resolve your problem.

Connectez-vous pour commenter.

Réponses (1)

Paul
Paul le 11 Juil 2024 à 22:05
Hi Roop,
If you want to use global data inside Matlab Function blocks, start from Use Global Data in MATLAB Function Blocks to assess if any of the provided methods are sufficient for your use-case.
I do not know if the Matlab Function blocks support use of user-defined classes (UDC) as seems to be the case here. Even if so, I'm not sure if a UDC instance can be used as a global object, and, frankly, I suspect that won't pe possible because this Simulink paradigm is based on Simulink.Signal objects. If it isn't there may be other workarounds.

Catégories

En savoir plus sur Simulink Coder dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by