Error while linking "C" with MATLAB

Hello to everyone,I'm new in this forum and this is my first thread.I have shannon fano's C program which i need to call in matlab and execute it in matlab.I modified some part of it to make MEX file.I create ".dll"(using windows XP) file by setting up MEX function then called it from matlab but i didn't get output,there is no errror in the program but still i didn't get output.The following is my C program which is perfectly working in C :
/* PROGRAM FOR SHANNON-FANO CODING TECHNIQUE (FOR ENCODING) */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
struct sh
{
char b;
float a;
}sn[50];
struct sh temp;
void main()
{
int k,v,i,fla,count,count1,q,j,p,bits1[40],h,f,kl,bits2[40];
float bits[40],l,l1,l2,r,n,l4,l3,g[40][40];
int g1[20][20],visit[20],visit1[20],g2[20][20]={1};
do
{
clrscr();
/****** initialising all the arrays ********* */
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
visit[i]=1;
visit1[i]=0;
g2[i][j]=1;
}
}
/* ******** ACCEPTING SYMBOLS & PROBABILITIES FROM USER ********* */
printf("how many number of symbols are there?");
scanf("%d",&q);
printf("enter symbols:\n");
flushall();
for(i=0;i<q;i++)
{
scanf("%c",&sn[i].b);
flushall();
}
printf("\nenter probabilities:\n");
for(i=0;i<q;i++)
{
scanf("%f",&sn[i].a);
}
/* ****** SORTING & ARRANGING IN DESCENDING ORDER ************ */
for(i=0;i<q;i++)
{
for(j=0;j<q;j++)
{
if(sn[j].a<sn[j+1].a)
{
temp=sn[j];
sn[j]=sn[j+1];
sn[j+1]=temp;
}
}
}
/* CALCULATING LENGTH OF CODE */
for(i=0;i<q;i++)
{
bits[i]=(float)((log10(1)-log10(sn[i].a))/(log10(2)));
bits1[i]=((log10(1)-log10(sn[i].a))/(log10(2)));
l2=bits[i]-bits1[i];
if(l2>0.5)
bits2[i]=bits1[i]+1;
else
bits2[i]=bits1[i];
}
/* *** CALCULATION OF ENTROPY, AVGLENGTH,CODE EFFICIENCY &REDUNDANCY **** */
l=0;
for(i=0;i<q;i++)
{
l=l+(sn[i].a)*((log10(1)-log10(sn[i].a))/(log10(2)));
}
printf("\n\nEntropy(H(X))=%f bits/symbol",l);
l1=0;
for(i=0;i<q;i++)
{
l1=l1+sn[i].a*bits2[i];
}
printf("\n\navgLength=%f ",l1);
n=(l/l1)*100;
printf("\n\ncode efficiency=%f %",n);
r=1-(n/100);
printf("\n\nredundancy(R)=%f",r);
/********************************************************* */
/* **********SEPERATING SYMBOLS ON THE BASIS OF PROBABILITIES ***/
for(i=0;i<q;i++)
visit[i]=0;
for(i=0;i<40;i++)
{
for(j=0;j<40;j++)
{
g[i][j]=0;
}
}
i=1;
j=0;
k=0;
l4=sn[0].a;
l3=0;
f=1;
while(l4>=sn[q-1].a)
{
h=f-1;
if(visit[h]!=1)
{
while(l3<(1/pow(2,i)))
{
l3=l3+sn[h].a;
g[k][j]=sn[h].a;
visit[h]=1;
j++;
h++;
if(j==2)
break;
}
k++;
i++;
}
if(f<=q)
{
j=0;
f++;
l4=sn[i-1].a;
l3=0;
}
if(f>q)
break;
}
printf("\n\n\npartition matrix is:\n");
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
printf("%f ",g[j][i]);
}
printf("\n");
}
/* ******* ASSIGNING 1'S & 0'S ACCORDING TO PARTITION MATRIX ****** */
for(i=0;i<q;i++)
{
for(j=0;j<q;j++)
{
g1[i][j]=1;
}
}
v=0;
i=0;
while(i<q-1)
{
for(j=0;j<q-1;j++)
{
if(g[v][j]!=0)
{
if(visit1[i]!=0)
i++;
g1[v][i]=0;
visit1[i]=1;
}
}
v++;
i++;
}
count=0;
fla=0;
for(i=0;i<q;i++)
{
for(j=0;j<q;j++)
{
if(g1[i][j]!=1)
{
kl=j;
count++;
}
}
if(count==2 && fla==0)
{
g2[i+1][kl-1]=0;
fla=1;
}
count=0;
fla=0;
}
for(i=0;i<q;i++)
{
for(j=0;j<q;j++)
{
if(g2[i][j]!=0)
g2[i][j]=g1[i][j];
}
}
printf("\n\n");
printf("\nno. of selections:\n");
/* ******** DISPLAYING TRANSITION MATRIX ******** */
count1=0;
for (i=0;i<q;i++)
{
for(j=0;j<bits2[q-1];j++)
{
if(count1<bits2[i])
{
printf("%d ",g2[j][i]);
count1++;
}
}
printf("\n");
count1=0;
}
printf("\n\n");
/* ****** DISPLAYING ALL PARAMETERS WITH LENGTH OF CODE & ASSIGNED CODE ***
*/
count1=0;
printf("\n\nsymbols\tp(Xi)\t length of code\tapprx.length of code\tcode\n");
for(i=0;i<q;i++)
{
printf("%c\t%f\t%f\t%d\t",sn[i].b,sn[i].a,bits[i],bits2[i]);
printf("\t\t");
for(j=0;j<bits2[q-1];j++)
{
if(count1<bits2[i])
{
printf("%d",g2[j][i]);
count1++;
}
}
printf("\n");
count1=0;
}
printf("\ndo u want to continue(y/n)?\n");
}while(getch()=='y' || getch()=='Y');
printf("\n\nThanx of using the code......\n\t\t\t\t- \n\npress any key to
EXIT\n");
getch();
}
And here is modified code for MATLAB:
/* PROGRAM FOR SHANNON-FANO CODING TECHNIQUE (FOR ENCODING) */
#include "mex.h"
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
struct sh
{
char b;
float a;
}sn[50];
struct sh temp;
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
int k,v,i,fla,count,count1,q,j,p,bits1[40],h,f,kl,bits2[40];
float bits[40],l,l1,l2,r,n,l4,l3,g[40][40];
int g1[20][20],visit[20],visit1[20],g2[20][20]={1};
do
{
/****** initialising all the arrays ********* */
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
visit[i]=1;
visit1[i]=0;
g2[i][j]=1;
}
}
/* ******** ACCEPTING SYMBOLS & PROBABILITIES FROM USER ********* */
mexPrintf("how many number of symbols are there?");
scanf("%d",&q);
mexPrintf("enter symbols:\n");
flushall();
for(i=0;i<q;i++)
{
scanf("%c",&sn[i].b);
flushall();
}
mexPrintf("\nenter probabilities:\n");
for(i=0;i<q;i++)
{
scanf("%f",&sn[i].a);
}
/* ****** SORTING & ARRANGING IN DESCENDING ORDER ************ */
for(i=0;i<q;i++)
{
for(j=0;j<q;j++)
{
if(sn[j].a<sn[j+1].a)
{
temp=sn[j];
sn[j]=sn[j+1];
sn[j+1]=temp;
}
}
}
/* CALCULATING LENGTH OF CODE */
for(i=0;i<q;i++)
{
bits[i]=(float)((log10(1)-log10(sn[i].a))/(log10(2)));
bits1[i]=((log10(1)-log10(sn[i].a))/(log10(2)));
l2=bits[i]-bits1[i];
if(l2>0.5)
bits2[i]=bits1[i]+1;
else
bits2[i]=bits1[i];
}
/* *** CALCULATION OF ENTROPY, AVGLENGTH,CODE EFFICIENCY &REDUNDANCY **** */
l=0;
for(i=0;i<q;i++)
{
l=l+(sn[i].a)*((log10(1)-log10(sn[i].a))/(log10(2)));
}
mexPrintf("\n\nEntropy(H(X))=%f bits/symbol",l);
l1=0;
for(i=0;i<q;i++)
{
l1=l1+sn[i].a*bits2[i];
}
mexPrintf("\n\navgLength=%f ",l1);
n=(l/l1)*100;
mexPrintf("\n\ncode efficiency=%f %",n);
r=1-(n/100);
mexPrintf("\n\nredundancy(R)=%f",r);
/********************************************************* */
/* **********SEPERATING SYMBOLS ON THE BASIS OF PROBABILITIES ***/
for(i=0;i<q;i++)
visit[i]=0;
for(i=0;i<40;i++)
{
for(j=0;j<40;j++)
{
g[i][j]=0;
}
}
i=1;
j=0;
k=0;
l4=sn[0].a;
l3=0;
f=1;
while(l4>=sn[q-1].a)
{
h=f-1;
if(visit[h]!=1)
{
while(l3<(1/pow(2,i)))
{
l3=l3+sn[h].a;
g[k][j]=sn[h].a;
visit[h]=1;
j++;
h++;
if(j==2)
break;
}
k++;
i++;
}
if(f<=q)
{
j=0;
f++;
l4=sn[i-1].a;
l3=0;
}
if(f>q)
break;
}
mexPrintf("\n\n\npartition matrix is:\n");
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
mexPrintf("%f ",g[j][i]);
}
mexPrintf("\n");
}
/* ******* ASSIGNING 1'S & 0'S ACCORDING TO PARTITION MATRIX ****** */
for(i=0;i<q;i++)
{
for(j=0;j<q;j++)
{
g1[i][j]=1;
}
}
v=0;
i=0;
while(i<q-1)
{
for(j=0;j<q-1;j++)
{
if(g[v][j]!=0)
{
if(visit1[i]!=0)
i++;
g1[v][i]=0;
visit1[i]=1;
}
}
v++;
i++;
}
count=0;
fla=0;
for(i=0;i<q;i++)
{
for(j=0;j<q;j++)
{
if(g1[i][j]!=1)
{
kl=j;
count++;
}
}
if(count==2 && fla==0)
{
g2[i+1][kl-1]=0;
fla=1;
}
count=0;
fla=0;
}
for(i=0;i<q;i++)
{
for(j=0;j<q;j++)
{
if(g2[i][j]!=0)
g2[i][j]=g1[i][j];
}
}
mexPrintf("\n\n");
mexPrintf("\nno. of selections:\n");
/* ******** DISPLAYING TRANSITION MATRIX ******** */
count1=0;
for (i=0;i<q;i++)
{
for(j=0;j<bits2[q-1];j++)
{
if(count1<bits2[i])
{
mexPrintf("%d ",g2[j][i]);
count1++;
}
}
mexPrintf("\n");
count1=0;
}
mexPrintf("\n\n");
/* ****** DISPLAYING ALL PARAMETERS WITH LENGTH OF CODE & ASSIGNED CODE ***
*/
count1=0;
mexPrintf("\n\nsymbols\tp(Xi)\t length of code\tapprx.length of code\tcode\n")
;
for(i=0;i<q;i++)
{
mexPrintf("%c\t%f\t%f\t%d\t",sn[i].b,sn[i].a,bits[i],bits2[i]);
mexPrintf("\t\t");
for(j=0;j<bits2[q-1];j++)
{
if(count1<bits2[i])
{
mexPrintf("%d",g2[j][i]);
count1++;
}
}
printf("\n");
count1=0;
}
mexPrintf("\ndo u want to continue(y/n)?\n");
}while(getch()=='y' || getch()=='Y');
mexPrintf("\n\nThanx of using the code......\n\t\t\t\t- \n\npress any key to
EXIT\n");
getch();
}
Can you please suggest any changes in it.?

Answers (1)

Kaustubha Govind
Kaustubha Govind on 18 Apr 2011
The title of your question suggests a linking error, but your questions points to a runtime issue. If you do see a linking error, please paste the exact error message. If it is a runtime issue, try debugging your MEX-function to find the exact cause of the problem.

Categories

Asked:

on 18 Apr 2011

Community Treasure Hunt

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

Start Hunting!