C++ Generated Code (from MATLAB)
So I generated some code from MATLAB, but I don't know how to use it. What I mean is if I wanted this to generate 15 (num2Gen) random numbers and perform the sum/avg calculation, how should I declare *a and *avg and then call the fxn?
void randAvg(uint16_T num2Gen, emxArray_uint16_T *a, uint16_T *avg)
{
int32_T i0;
int32_T loop_ub;
uint16_T sum;
uint16_T i;
real_T varargin_1;
uint16_T u0;
uint32_T u1;
/* RANDAVG Summary of this function goes here */
/* Detailed explanation goes here */
/* 'randAvg:4' a = fi(zeros(num2Gen,1),0,16,2); */
i0 = a->size[0];
a->size[0] = num2Gen;
emxEnsureCapacity((emxArray__common *)a, i0, (int32_T)sizeof(uint16_T));
loop_ub = num2Gen - 1;
for (i0 = 0; i0 <= loop开发者_开发技巧_ub; i0++) {
a->data[i0] = 0;
}
/* 'randAvg:5' sum = fi(0,0,16,2); */
sum = 0;
/* 'randAvg:6' for i=1:num2Gen */
for (i = 1; i <= num2Gen; i = (uint16_T)((uint32_T)i + 1U)) {
/* 'randAvg:7' a(i) = fi((50 + 100.*rand(1)),0,16,2); */
varargin_1 = 50.0 + 100.0 * b_rand();
varargin_1 = ldexp(varargin_1, 2);
varargin_1 = floor(varargin_1 + 0.5);
if (varargin_1 < 65536.0) {
if (varargin_1 >= 0.0) {
u0 = (uint16_T)varargin_1;
} else {
u0 = 0;
}
} else {
u0 = MAX_uint16_T;
}
a->data[i - 1] = u0;
/* 'randAvg:8' sum = fi((sum + fi(a(i),0,16,2)),0,16,2); */
u1 = (uint32_T)sum + (uint32_T)a->data[i - 1];
if (u1 > 65535U) {
u1 = 65535U;
}
sum = (uint16_T)u1;
/* don't use the array look up twice... */
}
/* 'randAvg:10' avg = fi(sum/num2Gen,0,16,2); */
*avg = (uint16_T)div_u32_near((uint32_T)sum, (uint32_T)num2Gen);
}
Thanks!
I don't know MATLAB, but to answer your question on how to pass in a reference to a function:
emxArray_uint16_T a = <put something here>
uint16_T avg = <put something here>
...
randAvg(num2Gen, &a, &avg);
精彩评论