Simulink error: The NAME input must be a constant

4 visualizaciones (últimos 30 días)
Stanley Martin Siagian
Stanley Martin Siagian el 6 de Feb. de 2023
Editada: Walter Roberson el 13 de Jun. de 2025
Sorry for the trivial question. I was coding my intergeneration time action in entity generator block in Simulink. I was certaint this bit of code will work because I tested it in Matlab. But instead I got this error from the diagnostic viewer.
Error in 'task06/Entity Generator ' block. See Intergeneration time action line 5, column 6.
Caused by:
The NAME input must be a constant.
Component:Simulink | Category:Block error
This is my code:
u = repelem([0.1; 5; 10; 15; 20; 35],[204 145 18 2 1 1]);
pd = fitdist(u,"Exponential");
dt = random(pd)
I wonder what's wrong? I ran it on Matlab and it worked everytime. TIA.
  5 comentarios
Walter Roberson
Walter Roberson el 6 de Feb. de 2023
I notice that if you
help random
then it talks strictly about "Name" as the first parameter. That leads me to suspect that at that point in the code, Simulink thinks you are invoking
toolbox/stats/stats/random.m
when instead you are invoking a method inside
toolbox/shared/statslib/+prob/TruncatableDistribution.m
As a debugging step, I suggest trying
u = repelem([0.1; 5; 10; 15; 20; 35],[204 145 18 2 1 1]);
pd = fitdist(u,'Exponential');
disp(which('random(pd)'))
dt = 0; %so you can actually execute without it complaining about dt not being defined
Stanley Martin Siagian
Stanley Martin Siagian el 7 de Feb. de 2023
Editada: Stanley Martin Siagian el 7 de Feb. de 2023
Sorry for the late response. I've done exactly as you written but apparently Simulink doesn't support the 'which' and the 'help' function.
Error in 'task06/Entity Generator ' block. See Intergeneration time action line 1, column 1.
Caused by:
  • Function 'help' not supported for code generation.
Component:Simulink | Category:Block error
Error in 'task06/Entity Generator ' block. See Intergeneration time action line 5, column 6.
Caused by:
  • Function 'which' not supported for code generation.
Component:Simulink | Category:Block error

Iniciar sesión para comentar.

Respuestas (1)

Tridib
Tridib el 13 de Jun. de 2025
I verified that Simulink is interpreting the call to “random” as referring to the function in “toolbox/stats/stats/random.m” which expects "Name" as the first input rather than the method from “toolbox/shared/statslib/+prob/TruncatableDistribution.m”.
1. As an alternative, the distribution can be precomputed and samples can be generated in advance:
% in MATLAB Workspace, before simulation
u = repelem([0.1; 5; 10; 15; 20; 35],[204 145 18 2 1 1]);
pd = fitdist(u, "Exponential");
precomputed_dt = random(pd, 1000,1); % generate a big vector of samples
assignin('base','precomputed_dt',precomputed_dt); % put into base workspace
2. Within the Intergeneration time action, those precomputed values can then be used as follows:
% persistent idx keeps the value of idx between calls, so it remembers
% which part of precomputed_dt to use next during the simulation.
persistent idx;
if isempty(idx)
idx = 1;
end
dt = precomputed_dt(idx);
idx = idx + 1;
if idx > length(precomputed_dt)
idx = 1; % Loop
end
3. If your goal is just "random exponential delay", you can replace your code with:
dt = exprnd(mean(u));
For more help, refer to the following documentation:
Hope this helps!

Categorías

Más información sobre General Applications en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by