Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading A matrix created in MATLAB ?

Status
Not open for further replies.

Pushhead

Technical User
May 3, 2003
3
IL
Hi All!

I'm trying 2 read a text file containing a N X 2 Matrix created in matlab.
The file was created in matlab using :
Code:
 n=1:3;
a=exp(j*n);
y=[real(a);imag(a)];
fid = fopen('cplx1.txt','w');
fprintf(fid,'%g %g\n',y);
fclose(fid);

I tried 2 read the file using (TClite):
Code:
....
}
 while (!feof(fid))
 {
  fscanf(fid,"%g %g",&array[i][0],&array[i][1]);
  printf("x[%d]= %g+j%g \n",i,array[i][0],array[i][1]);
  i++;
  }
 }...
I get the error msg: "scanf: Floating point formats not linked, Abnormal program termination".
Where is the problem?
is it C or MATLAB?
Thanks 4 your time.
 
Your compiler is too dumb to spot that you need floating point support

Various "hacks" are needed to make it realise this, eg create the following global
float dummy = 1.0;

> while (!feof(fid))
This is the wrong way - you'll find it processes the last line "twice"

Should be
Code:
while ( fscanf(fid,"%g %g",&array[i][0],&array[i][1]) == 2 ) {
    printf("x[%d]= %g+j%g \n",i,array[i][0],array[i][1]);
    i++;
}
 
I tried your suggestion, but i still get
the same error.

can u think of something else?

thanks.
 
If you can handle the bounce at the end of the file,
"while (!feof(fid))" will work fine.

I have good luck with something like this. Sometimes you have to fiddle with the if statement below to exit properly, depending upon your intentions.

bool done = false;

while (!done)
{
fscanf(fid,"%g %g",&array[0],&array[1]);
printf("x[%d]= %g+j%g \n",i,array[0],array[1]);
i++;

if (feof (fid))
{
done = true;
}
}

tomcruz.net
 
first of all thanks alot 4 ur help,
it just shows people are SERIOUS here, in comparison 2 other forums i've visited....


but:
the error messege has disappeared, but the values i get r not the same.
the data file( text format) contains the matrix:
0.540302 0.841471
-0.416147 0.909297
-0.989992 0.14112

and what i get is:
x[0]=-9.25526e+061+j-9.25526e+061
x[1]=-9.25526e+061+j-9.25526e+061
x[2]=-9.25526e+061+j-9.25526e+061
x[3]=6.25526e-314+j-9.25526e-314

can u help?
10x alot.
 
I'm guessing array is an array of double, not an array of float

In which case, use "%lg" (not "%g") in the scanf() conversion string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top