Put float data into mat-file using fwrite function

4 次查看(过去 30 天)
I am writing a program in C, its job is: get datas from txt file and put them into a mat file. I use fwrite function to write the data, codes as below:
fwrite(&data, typesize, 1750, pf);
but it doesn't always works, when the data type is float, it may cause the mat file load failure.
??? Error using ==> load
Unable to read MAT-file
D:\out.mat
File may be corrupt.
e.g.
if it writes 1750 datas like 1.005312, the file corrupt.
if it writes 1750 datas like 0.369306 or 1699300.000000, it works very well.
Is the type float in C the same to Single in matlab? or How to fix it?
Kindly help!

采纳的回答

R Zh
R Zh 2012-8-20
Solved, the mat-file should be a binary file, codes as bellow,
pf = fopen( OutFileName, "wb+");
Thanks all the same.
  2 个评论
Walter Roberson
Walter Roberson 2012-8-20
Jan, notice that double-quotes were used in the wb+ string. That gives us the clue that the line is C or C++ code, not MATLAB code.
Also, .mat files can be accessed with fwrite() . It is more often a nuisance to do so for variables that were compressed in place, but compression is optional and variables that are not compressed are in binary format suitable for fwrite()
Jan
Jan 2012-8-20
@Walter: I obviously misunderstood the intention of the OP. Of course MAT file can be written using FWRITE, because this command can create every file, which can be created. But this is neither secure nor comfortable. I've deleted my comment which unfortunately reduces the power of your reply. Sorry.

请先登录,再进行评论。

更多回答(1 个)

José-Luis
José-Luis 2012-8-17
If you really want to create a mat file from scratch, then you should read the white paper first:
It is not really straigthforward, I think you are better off using the Matlab C++ API:
Cheers!

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by