Wrong Values of Doubles from .mat file in C

3 views (last 30 days)
Tony Whittam
Tony Whittam on 28 Mar 2023
Answered: Tony Whittam on 29 Mar 2023
I have a C program reading a .mat file. The file contains 2 1d arrays of doubles. The program gets a pointer to the beginning of an array of doubles by using mxGetDoubles(). It can successfully read all the values, however there appears to be a scaling problem when printing them with printf("%lf\n", ...) , e.g.
13.69798 displays as as 24.553575
48.94982 displays as as 57.016571
Matlab is R2022a
GCC is x86_64 V11.3.0 (MINGW running in Windows)
Could there be a difference in encoding? I thought they would both use IEEE 754 double encoding.
  2 Comments
Walter Roberson
Walter Roberson on 28 Mar 2023
?? mxGetDoubles() is not (in itself) suitable for reading doubles from a .mat file.
Tony Whittam
Tony Whittam on 29 Mar 2023
Edited: Tony Whittam on 29 Mar 2023
Perhaps I should have given more detail. Here is the C code ...
const char *file1 = "matlab/test/testData/testfile.mat";
MATFile *pmat;
mxArray* pa;
mxArray* pa1;
mxDouble* extData;
pmat = matOpen(file1, "r");
pa1 = matGetNextVariableInfo(pmat, &name);
pa = matGetVariable(pmat, name);
extData = mxGetDoubles(pa);
for (i=0; i<32; i++)
{
printf("%lf\n", extData[i]);
}

Sign in to comment.

Answers (1)

Tony Whittam
Tony Whittam on 29 Mar 2023
I just solved our problem. Operator error as usual. We were looking at different data files in Matlab and the C code. Once I opened the same file in Matlab, the numbers were identical to those printed in C.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!