Calls to coder.extrinsic in inner-level of Matlab/Simulink

Hi. I need to generate random numbers in normal distribution in Simulink. I use this code in one of my functions:
coder.extrinsic('random');
x=random('rand',0,1);
but I'm getting this error:
Calls to coder.extrinsic may only appear at the top-level.
Is there anyone to help me?

 Réponse acceptée

Walter Roberson
Walter Roberson le 27 Juil 2015
Is the coder.extrinsic() call within a loop or control structure? If so then move it to be outside those (but still in the function.)

7 commentaires

Yes. It's a control Structure with loop but I need to generate the random number inside a function because the sigma needs to be refreshed in each time step regarding to a particular variable. How can I move the code outside if I need to generate it inside ?
What Walter means is instead of:
function y = foo(x)
y = zeros(1,numel(x));
for k = 1:numel(x)
% inside of control structure
coder.extrinsic('random');
y(k) = random('rand',0,1);
end
use:
function y = foo(x)
y = zeros(1,numel(x));
% Outside of control structure
coder.extrinsic('random');
for k = 1:numel(x)
y(k) = random('rand',0,1);
end
The same thing goes for while, switch, and if constructs.
Marie Behzadi
Marie Behzadi le 29 Juil 2015
Modifié(e) : Marie Behzadi le 29 Juil 2015
Thanks for your reply. I did what you meant. but the error changed !!! I even tried to use "normrnd" function. but i still get the same error.
You did not happen to show us the code.
Probably you have something of the form
SomeRoutine( SomeOtherRoutine(Arguments) )
You will need to convert that to
temp = SomeOtherRoutine(Arguments);
SomeRoutine(temp)
Thanks Ryan. I used what you and Walter meant. and the first error disappeared. The new error is when I want to use this random number. In other words, when I use this code I get no error:
rho=1;
noise=normrnd(0,rho);
but when I add it to some variable, I get new errors:
rho=1;
noise=normrnd(0,rho);
temp=noise+1;
This is the error:
Expected either a logical, char, int, fi, single, or double. Found an mxArray. MxArrays are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may only be used on the right-hand side of assignments and as arguments to extrinsic functions.
The term "extrinsic" means you're going outside of the normal system to get the job done. Here, you're contracting out evaluation to MATLAB like some kind of server, and MATLAB communicates to its clients using a data type called an "mxArray". It has data inside it, but that data can be anything. The mxArray itself tells what's inside of it, which would be fine if the MATLAB Function Block could wait until you actually call the function to decide what to do. It can't, however. It must figure out what will be inside of the array before the simulation even starts. The way you tell the block that is by pre-defining the variable you will use to hold the output of the extrinsic function call.
Since this extrinsic function call will return a scalar double value, in this case you would use
rho = 1
noise = 0;
noise = normrnd(0,rho);
temp = noise + 1;
But why do you need to use an extrinsic call for a random normal? Just use the RANDN function like so:
noise = mu + sd*randn;
Or if you upgrade to R2013b, you can call NORMRND or RANDOM without making it extrinsic.
Thanks. It worked. I will try to upgrade to R2013b. but the first solution you described just worked fine. Thank you all :)

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 7 Août 2015
There's a block to do this. Perhaps you can pass the signal from this block into your function as an input?

1 commentaire

I tried this method. but I need to change the variance each time step. When I use this block and I read the variance from workspace, It only uses the first value. It also doesn't have an input port.

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by