How to create an all zero array whose dimensions are defined by the Simulink parameters in the data dictionary?

Hello everyone,
I am learning to use Simulink to build some software functions, and I have used the data dictionary to manage my general macro definitions.
I have defined a Simulink parameter N in the data dictionary, and now I want to create an all zero array of N and generate C code using the embedded Coder tools.
My current method is to use the constant module, and also use "=zeros (1, N)" in the data dictionary to define a simulink parameter A, and call this parameter in the constant module.
but I am prompted with the following error message when generating code. Is there any good way to solve it?
The error message as follow:
Unable to preserve expression 'zeros(1,N)', contained in the 'Value' property of Simulink.Parameter object 'A', in the generated code because the expression or its value is not supported.
Thanks

7 Comments

I tried to recreate the error using the setup shown in the attached screenshot, but I wasn't able to reproduce the error.
Could you please provide more details about the model and its settings?
Hi,
Thank you for your reply.My description omitted that N is also a Simulink parameter. Because I want N to appear in the form of #Define in C code.
In which version of MATLAB are you encountering the error?
I tried the attached simulink file and again was not able to reproduce any error in R2021b, Please check the attached screenshot.
For troubleshooting can you please check your base workspace for any conflicting variable names and If that doesn't work then please share the data dictionary file for better analysis.
hi,
This is a confusing question,please refer to the test model I uploaded.The version is R2021B.
Which operating system is being used to generate the .c code from the attached model?
I tried using the same test model for code generation on a Windows 10 computer, but received the same error message.

Sign in to comment.

Answers (1)

Hi,
It appears that you are attempting to generate C code from the 'test.slx' Simulink file in MATLAB R2021b and you are encountering an error. To address this issue, you can adjust the Model Configuration Parameters to disable the Data Validity error check for "loss of tunability" (refer to the attached 'Screenshot_Model_Settings.png') .
Following are the steps to change the setting:
  1. Open the "Model Settings" from the Modeling toolstrip.
  2. Navigate to Diagnostics > Data Validity.
  3. In the parameters section, change the setting for "loss of tunability" from "error" to either "warning" or "none.".
Following these steps should resolve the error, allowing you to generate the C file using Embedded Coder.
To ensure the ‘N’ variable is included in the generated C file, you should add additional blocks to the Simulink model (refer to the attached 'test.slx' file) and then proceed with generating the C code (refer to the attached 'Screenshot_code_generated.png' file).
Please refer to the below documentation for more information.
  1. 'Detect loss of tunability’: https://www.mathworks.com/help/releases/R2021b/simulink/gui/detect-loss-of-tunability.html
  2. ‘Limitations for Block Parameter Tunability in Generated Code’: https://www.mathworks.com/help/releases/R2021b/rtw/ug/limitations-for-block-parameter-tunability-in-the-generated-code.html
I hope this helps.

1 Comment

Thank you for your guidance, it has resolved my issue of constantly reporting errors. But I found that there seems to be no zero assignment operation for the A array I created in the generated C code. Do I need to create an additional loop assignment module to initialize the value of A? I searched the help documentation and it seems that the zeros function supports code generation. This is the place that confuses me.

Sign in to comment.

Products

Release

R2021b

Asked:

on 5 Sep 2024

Commented:

on 7 Sep 2024

Community Treasure Hunt

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

Start Hunting!