how to run c program file in matlab

1 回表示 (過去 30 日間)
SHOBA MOHAN
SHOBA MOHAN 2018 年 1 月 10 日
コメント済み: SHOBA MOHAN 2018 年 1 月 11 日
Hi i wish to call function written in .c in matlab. I installed sdk. I tried with mex filename.c and got the following error
C:\Users\Deepa\Documents\MATLAB\treevalc.c(26) : warning C4101: 'cid' : unreferenced local variable
C:\Users\Deepa\Documents\MATLAB\treevalc.c(92) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(94) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(95) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(97) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(100) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(101) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(102) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(104) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(105) : error C2065: 'n' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(105) : error C2065: 'n' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(105) : error C2065: 'nsplits' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(105) : error C2065: 'n' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(106) : error C2065: 'n' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(110) : error C2065: 'ncatsplit' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(110) : error C2065: 'n' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(110) : error C2109: subscript requires array or pointer type
C:\Users\Deepa\Documents\MATLAB\treevalc.c(111) : error C2065: 'catsplit' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(111) : error C2065: 'n' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(111) : error C2109: subscript requires array or pointer type
C:\Users\Deepa\Documents\MATLAB\treevalc.c(114) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(115) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(119) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(121) : error C2065: 'numdata' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(122) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(125) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Deepa\Documents\MATLAB\treevalc.c(126) : error C2065: 'n' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(126) : error C2065: 'n' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(126) : error C2065: 'numdata' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(126) : error C2065: 'n' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(127) : error C2065: 'var' : undeclared identifier
C:\Users\Deepa\Documents\MATLAB\treevalc.c(127) : warning C4047: 'function' : 'int *' differs in levels of indirection
from 'int'
I am able to call successfully .cpp file in matlab but getting error while using .c file. I am using windows 10 matlab r2014a.
  4 件のコメント
SHOBA MOHAN
SHOBA MOHAN 2018 年 1 月 10 日
Thanks for the answer. I tired with that and got invalid option. I am intended to use the c program function into matlab. For your reference i am attaching my .c file code. Please suggest the way forward
#include "mex.h"
#include <stdlib.h>
#include <stdio.h>
/**
* Return the decision tree node corresponding to the given value set
*
* var[n]: the attribute ids for node n
* cut[n]: the threshold value for node n
* left_child[n]: the node id of the left child of node n, 0 if node n is terminal
* right_child[n]: the node id of the right child of node n, 0 if node n is terminal
* ncatsplit[c]: the number of values resulting in a left branch
* catsplit[c]: the values that would result in a left branch
* attributes: the attribute (variable) values for each feature
**/
void
treevalc(int* var, double* cut, int* left_child, int* right_child,
int* ncatsplit, double** catsplit,
double* attributes,
int* node_id) {
int currnode = 0;
int nextnode;
int currvar;
double currval;
int cid, v;
int numvals;
double* vals;
/* printf("init nodes: %d %d \n", left_child[currnode], right_child[currnode]); */
/* until reached terminal node */
while ((left_child[currnode] != 0) && (right_child[currnode] != 0)) {
/*printf("currnode: %d\n", currnode);*/
nextnode = -1;
currvar = abs(var[currnode])-1;
currval = attributes[currvar];
/* decision based on thresholded float value */
if (var[currnode] > 0) {
/*printf("currvar: %d\n", currvar);*/
/* branch left */
if (currval < cut[currnode]) {
nextnode = left_child[currnode];
}
/* branch right */
else {
nextnode = right_child[currnode];
}
}
/* decision based on discrete value */
else {
numvals = ncatsplit[(int)cut[currnode]-1];
vals = catsplit[(int)cut[currnode]-1];
for (v = 0; v < numvals; v++) {
if (currval == vals[v]) {
nextnode = left_child[currnode];
break;
}
}
if (nextnode == -1) {
nextnode = right_child[currnode];
}
}
currnode = nextnode-1;
/* printf("curr node: %d \n", currnode);*/
}
*node_id = currnode+1;
}
/**
* plhs = {var, cut, left_child, right_child, catsplit(cell array), attributes(numatt, numdata)}
*
*/
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
if (nrhs != 6) {
printf("Error: wrong number of input arguments: %d.\n", nlhs);
printf("Syntax: node_ids = treevalc(var, cut, left_child, right_child, catsplit, attributes)\n");
}
int* var = (int*)mxGetData(prhs[0]);
/* int* var = (int*)mxGetPr(prhs[0]); */
double* cut = mxGetPr(prhs[1]);
int* left_child = (int*)mxGetData(prhs[2]);
/* int* left_child = (int*)mxGetPr(prhs[2]); */
int* right_child = (int*)mxGetData(prhs[3]);
/* int* right_child = (int*)mxGetPr(prhs[3]); */
/* get catsplit variables */
int nsplits = mxGetNumberOfElements(prhs[4]);
int* ncatsplit = malloc(sizeof(int) * nsplits);
double** catsplit = malloc(sizeof(double*) * nsplits);
int n = 0;
for (n = 0; n < nsplits; n++) {
mxArray* catsplit_cell_mx = mxGetCell(prhs[4], n);
if (catsplit_cell_mx == 0) {
printf("null cell");
}
ncatsplit[n] = mxGetNumberOfElements(catsplit_cell_mx);
catsplit[n] = (double*)mxGetPr(catsplit_cell_mx);
}
int numatt = mxGetM(prhs[5]);
int numdata = mxGetN(prhs[5]);
/* printf("num data = %d num att = %d\n", numdata, numatt);*/
double* all_attributes = mxGetPr(prhs[5]);
plhs[0] = mxCreateDoubleMatrix(numdata, 1, mxREAL);
double* node_ids = mxGetPr(plhs[0]);
int tmp_id;
for (n = 0; n < numdata; n++) {
treevalc(var, cut, left_child, right_child, ncatsplit, catsplit,
&all_attributes[numatt*n], &tmp_id);
node_ids[n] = (double)(tmp_id);
/* printf("final node id: %d\n", tmp_id); */
}
free(catsplit);
free(ncatsplit);
}
SHOBA MOHAN
SHOBA MOHAN 2018 年 1 月 10 日
@ Walter Please refer the attached file and provide your valuable input.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 1 月 10 日
Near line 87 you have executable code before you declare some of your local variables. That is valid C++, and it is valid in later C releases, but it is not valid in the default compilation model of C89.
You will need to do one of the following:
  • move those lines to after the initialization; or
  • put a {} around that initialization and to the rest of the function, so that the initializations are the first thing within a block; or
  • add the -std=c99 compile option; or
  • if you are using the GUI to set up the compile, find the place in the GUI to mark that you want C99 or C11.
  1 件のコメント
SHOBA MOHAN
SHOBA MOHAN 2018 年 1 月 11 日
Thanks Walter. I tried with first two option got the following error
Undefined function 'treevalc' for input arguments of type 'cell'.
Which part shall i include -std=c99 compile option? I am not using GUI.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by