[MatLab] come fare funzionare un programma in C

tommy_2222
Salve a tutti, non ho capito come si fa a fare funzionare un programma scritto in c da parte di Matlab come se fosse una normale funzione .m

Per esempio un programma banale

#include

void funzione()
{
int n;

printf("inserisci un numero");
scanf("%d", &n);
printf("hai inserito %d", n);
}

Come si fa a farlo leggere da matlab?

Risposte
tommy_2222
Rispondo da solo alla mia domanda. Ci sono riuscito anche se non ho capito bene come ha lavorato matlab(lo uso da poco).

Passi:

1) ho copiato il programma
/*=========================================================================
* unravel.c
* Decodes a variable length codes bit sequence (a vector of
* 16-bit integers) using a binary sort from the MSB to the LSB
* (across word boundaries) based on a transition table.
*=======================================================================*/
#include "mex.h"
void unravel(unit16_T *hx,double *link,double *x, double xsz,int hxsz)
{
int i=15, j = 0, k = 0, n = 0; /* Start at root node, 1st */
/* hx bit and x element */
while (xsz - k) { /* Do until x is filled */
if (*(link + n) > 0) { /* Is there a link? */
if ((*(hx + j) >> i) & 0x0001) /* Is bit a 1? */
n = *(link + n); /* Yes, get new node */
else n = *(link + n) - 1; /* It's 0 so get new node */
if (i) i--; else {j++; i = 15;} /* Set i, j to next bit */
if (j > hxsz) /* Bits left to decode? */
mexErrMsgTxt("Out of code bits ???");
}
else { /* It must be a leaf node */
*(x + k++) = -*(link + n); /* Output value */
n = 0; } /* Start over at root */
}
if (k == xsz - 1) /* Is one left over? */
*(x + k++) = -*(link + n);
}
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *link, *x, xsz;
uint16_T *hx;
int hxsz;
/* Check inputs for reasonableness */
if (nrhs != 3)
mexErrMsgTxt("Three inputs required.");
else if (nlhs > 1)
mexErrMsgTxt("Too many output arguments.");
/* Is last input argument a scalar? */
if(!mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]) || mxGetN(prhs[2]) * mxGetM(prhs[2]) != 1)
mexErrMsgTxt("Input XSIZE must be a scalar.");
/* Create input matrix pointers and get scalar */
hx = (uint16_T *) mxGetData(prhs[0]);
link = (double *) mxGetData(prhs[1]);
xsz = mxGetScalar(prhs[2]); /* returns DOUBLE */
/* Get the number of elemnts in hx */
hxsz = mxGetM(prhs[0]);
/* Create 'xsz' x 1 output matrix */
plhs[0] = mxCreateDoubleMatrix(xsz, 1, mxREAL);
/* Get C pointer to a copy of the output matrix */
x = (double *) mxGetData(plhs[0]);
/* Call the C subroutine */
unravel(hx, link, x, xsz, hxsz);
}

in un blocco note cambiando l'estenzione del file in .c (ovviamente era .txt)
Ho chiamato il file: unravel.c

2) ho messo questo file nella current folder

3) ho digitato nel prompt di matlab:
mex unravel.c
e ho digitato il numero 1

4)a questo punto matlab ha creato nella current folder il file unravel.mexw32

5) qualunque funzione scritta in .m che chiama unravel adesso funziona
tipo un comando x = unravel(y.code', link, m * n); adesso funziona

elgiovo
Non sono un esperto su questo argomento ma so che la procedura è bene o male quella seguita da te, ovvero far compilare il programma C a Matlab (tramite qualche compilatore che gli è stato indicato in fase di installazione, tipo gcc) e creare un file MEX, da usare negli script o addirittura come un blocco simulink. Ti linko una pagina dell'help di matlab che ti può essere utile (non mi sembra che l'hai fatto in modo molto "pulito".. :D )

Rispondi
Per rispondere a questa discussione devi prima effettuare il login.