Matlab Coder : codegen for mvnrnd

Hi, I'm trying to generate standalone C code of the mvnrnd function in Matlab with Matlab Coder. By doing so, I keep getting error:
>> mu = [1 -1]
mu =
1 -1
>> sigma = [.9 .4; .4 .3]
sigma =
0.9000 0.4000
0.4000 0.3000
>> codegen -config:lib -report -c mvnrnd1.m -args {mu,sigma}
??? The input matrix must be variable-size in both dimensions when nargout == 2
How do I insert variable-size matrix in -args? Thank you!

Réponses (2)

Mike Hosea
Mike Hosea le 9 Avr 2012

0 votes

Instead of using sigma in that -args list, use
sigma_type = coder.typeof(0,[2,2],[true,true])
This means the type is double and real (that is the type of the first argument "0"). The second argument says that the array is up to 2-by-2, and the third argument says that both dimensions are variable in size.
Good luck. The conversion looks doable, but there may be other snags, and it's the type of function where I'd probably do a lot of re-writing to make it efficient for code generation (e.g., I'd write a little loop to calculate sum(D<0) instead of creating a logical array and then summing it). -- Mike

8 commentaires

Jane Jean
Jane Jean le 15 Avr 2012
Sorry for the late reply. I did not have a chance to continue on the work until now.
I'm currently modifying the original script of mvnrnd but the compilation still give an error :
??? The function 'message' is not supported for standalone code generation. See the documentation for coder.extrinsic to learn how you can use this function in simulation.
What does that mean ? My aim is to generate a Mex function. Is this a simulation?
Jane Jean
Jane Jean le 15 Avr 2012
error(message('stats:mvnrnd:TooFewInputs'));
is the line where the error occurs.
Walter Roberson
Walter Roberson le 15 Avr 2012
Earlier you said you were trying to generate standalone C code. Now you say your aim is to generate a Mex function. Mex functions are not for standalone use: they are for use within MATLAB.
How do you want your C code to act when it is given invalid input such as too few parameters?
Jane Jean
Jane Jean le 15 Avr 2012
In the coder tutorial, it says it's better to first test it with a mexfunction before generating the C code. That is why I want to generate a mexFunction now. But is this really necessary?
I would want the code to display the same message: TooFewInputs.
Thanks for your help.
Walter Roberson
Walter Roberson le 15 Avr 2012
The current .m call uses error(). Standalone C does not have any (useful) equivalent to error(). error() does not just display a message: it also causes returns back through the calling routines (destroying local variables if appropriate) until it finds a corresponding try/catch block.
Jane Jean
Jane Jean le 15 Avr 2012
Is there a way to make the generated code do this mexErrMsgIdAndTxt("TooFewInputs")?
Jane Jean
Jane Jean le 15 Avr 2012
I've added a line at the beginning of the function :
coder.extrinsic('cholcov', 'message');
Matlab crashes before generating the mexFunction. What had happened now?
Jane Jean
Jane Jean le 15 Avr 2012
mu = coder.typeof(0,[500,500],[true,true]);
sigma = coder.typeof(0,[500,500],[true,true]);
codegen -report mvnrnd1.m -args {mu, sigma};
Here above are the lines that i used to generate mexFunction.

Connectez-vous pour commenter.

Jane Jean
Jane Jean le 15 Avr 2012

0 votes

I have managed to the eliminate the errors which caused the crash by defining the size of the output of cholcov in mvnrnd.m script.
Now the mexFunction works perfectly. :) Thank you for your help.

1 commentaire

Mike Hosea
Mike Hosea le 16 Avr 2012
I'm glad, but I'm a little confused. If you are just building a mex function, why not make MVNRND extrinsic?

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by