I am trying my hands with Matlab coder. I wrote a simple matlab
script and generated the C-code (along with the necessary headers) for the same. The matlab script looks like this :
function amin_idx=findminidx(a)%#codegen
amin_idx=find(a==min(a),1,'first');
In the Matlab coder, I start a new project, pre-condition a
as a 10x1
vector and build the project to generate the .c
and .h
files. The I write a new .c
file containing the main()
which is as follows :
#include<stdio.h>
#include "rt_nonfinite.h"/*from generated code*/
#include "findminidx.h"/*from generated code*/
void main(void){
double a_data[3]={6,5,4};
int a_size[2]={3,1};
double amin_idx_data[1]={-1};
int amin_idx_size[2]={1,1};
findminidx(a_data,a_size,amin_idx_data,amin_idx_size);/*from generated code*/
printf("\namin_idx = %f\n",amin_idx_data[0]);
}
The .c
file (generated) containing the findminidx()
is given below :
/*
* File: findminidx.c
*
* MATLAB Coder version : 2.6
* C/C++ source code generated on : 30-Aug-2014 18:04:42
*/
/* Include files */
#include "rt_nonfinite.h"
#include "findminidx.h"
/* Function Definitions */
/*
* Arguments : const double a_data[]
* const int a_size[2]
* double amin_idx_data[]
* int amin_idx_size[2]
* Return Type : void
*/
void findminidx(const double a_data[], const int a_size[2], double
amin_idx_data[], int amin_idx_size[2])
{
int ixstart;
double mtmp;
int ix;
boolean_T exitg2;
boolean_T x_data[10];
int k;
int ii_data[1];
boolean_T exitg1;
int b_ii_data[1];
ixstart = 1;
mtmp = a_data[0];
if (a_size[1] > 1) {
if (rtIsNaN(a_data[0])) {
ix = 2;
exitg2 = false;
while ((!exitg2) && (ix <= a_size[1])) {
ixstart = ix;
if (!rtIsNaN(a_data[ix - 1])) {
mtmp = a_data[ix - 1];
exitg2 = true;
} else {
ix++;
}
}
}
if (ixstart < a_size[1]) {
while (ixstart + 1 <= a_size[1]) {
if (a_data[ixstart] < mtmp) {
mtmp = a_data[ixstart];
}
ixstart++;
}
}
}
ixstart = a_size[0] * a_size[1];
for (ix = 0; ix < ixstart; ix++) {
x_data[ix] = (a_data[ix] == mtmp);
}
if (1 <= a_size[1]) {
k = 1;
} else {
k = 0;
}
ixstart = 0;
ix = 1;
exitg1 = false;
while ((!exitg1) && (ix <= a_size[1])) {
if (x_data[ix - 1]) {
ixstart = 1;
ii_data[0] = ix;
exitg1 = true;
} else {
ix++;
}
}
if (k == 1) {
if (ixstart == 0) {
k = 0;
}
} else {
if (1 > ixstart) {
ixstart = -1;
} else {
ixstart = 0;
}
ix = 0;
while (ix <= ixstart) {
b_ii_data[0] = ii_data[0];
ix = 1;
}
k = ixstart + 1;
ixstart++;
ix = 0;
while (ix <= ixstart - 1) {
ii_data[0] = b_ii_data[0];
ix = 1;
}
}
amin_idx_size[0] = 1;
amin_idx_size[1] = k;
ixstart = k;
ix = 0;
while (ix <= ixstart - 1) {
amin_idx_data[0] = ii_data[0];
ix = 1;
}
}
/*
* File trailer for findminidx.c
*
* [EOF]
*/
The intention is, calling the .c
routine generated from matlab
, passing it a small array and printing the index of minimum value of the passed array. I put the c-file containg main()
in the same directory as the one containing all matlab
generated .c
and .h
files. Also, I included the respective headers in the main()
c file as can be seen above. When I try to compile using gcc file1.c findminidx.c
, it yields the following error.
undefined reference to `rtIsNaN'
I can not understand why is this? My system details are as follows :
- Ubuntu 14.04, 64 bit
- Matlab R2014a, 64 bit
- gcc version 4.8.2