Simulink error: The NAME input must be a constant

조회 수: 9 (최근 30일)
Stanley Martin Siagian
Stanley Martin Siagian 2023년 2월 6일
편집: Walter Roberson 2025년 6월 13일
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
Walter Roberson
Walter Roberson 2023년 2월 6일
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 2023년 2월 7일
편집: Stanley Martin Siagian 2023년 2월 7일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Tridib
Tridib 2025년 6월 13일
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!

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by