How can I guarantee Function input Data to be in a certain shape for Coder translated C Code?

1 回表示 (過去 30 日間)
I have the following Matlab Function:
function[Product] = computeDotProduct( vecA, vecB )
Product = double(0.0);
errors = char('');
%%Check Shape of Input Data
if( isrow(vecA) == false || iscolumn(vecB) == false)
errors = 'Input Vectors have wrong shape!';
return;
end
%%Compute the dot Product.
Product = vecA * vecB;
errors = 'OK';
end
I then use the Matlab Coder Tool box to translate the function into C Code with the following command:
codegen computeDotProduct.m -args {coder.typeof( double( 0.0 ), [ 1, Inf ], [ 0, 1 ] ), coder.typeof( double( 0.0 ), [ Inf,1 ], [ 1, 0 ] )}
As specified, the first argument vecA must be a row vector, vecB a column vector, respectively. The generated C Function has the following Signature:
void computeDotProduct(const emlrtStack *sp,
const emxArray_real_T *vecA,
const emxArray_real_T *vecB,
real_T *Product,
char_T errors_data[],
int32_T errors_size[2] )
where the two input structs vecA and vecB may be of arbitrary Size, because emxArray_real_T is not specifically a row or column vector. The body of the function looks like:
...
static const char_T cv0[2] = { 'O', 'K' };
...
/* %%Check Shape of Input Data */
/* %%Compute the dot Product. */
st.site = &emlrtRSI;
b_st.site = &c_emlrtRSI;
if (!(vecA->size[1] == vecB->size[0])) {
if ((vecA->size[1] == 1) || (vecB->size[0] == 1)) {
emlrtErrorWithMessageIdR2012b(&b_st, &b_emlrtRTEI,
"Coder:toolbox:mtimes_noDynamicScalarExpansion", 0);
} else {
emlrtErrorWithMessageIdR2012b(&b_st, &c_emlrtRTEI, "Coder:MATLAB:innerdim",
0);
}
}
if ((vecA->size[1] == 1) || (vecB->size[0] == 1)) {
...
}
} else {
...
}
...
for (i0 = 0; i0 < 2; i0++) {
errors_data[i0] = cv0[i0];
}
There is indeed a check for Dimensions, but the Shape-Check I put in was dropped by the Coder. So, my question is: Is it possible to prevent the coder from removing the input check but still keeping the coder.typeof statements as above?

回答 (1 件)

Tony Mohan Varghese
Tony Mohan Varghese 2018 年 3 月 21 日
Unbounded variable size inputs are defined as structures in the generated C code and it will be named as emxArray_real_T.
As you can see, the data and size are defined separately in the structure emxArray_real_T.
The shape-check that you specified is done by the following line in the generated code:
if ((vecA->size[1] == 1) || (vecB->size[0] == 1)) {
Product = 0.0;
for (i0 = 0; i0 < vecA->size[1]; i0++) {
Product += vecA->data[vecA->size[0] * i0] * vecB->data[i0];
}
} else {
Product = 0.0;
for (i0 = 0; i0 < vecA->size[1]; i0++) {
Product += vecA->data[vecA->size[0] * i0] * vecB->data[i0];
}
}
vecA->size[1] corresponds to the row of the vector A and similarily for vector B.

カテゴリ

Help Center および File ExchangeGenerating Code についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by