Read a binary file of format float

Hello, I have a binary file that holds a content of float array that wa written in c#. I want to read it into matlab to view that float array. I think I need to use the next command but it seems to fail:
myfloat=fread(fID,1,'float');

답변 (2개)

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2015년 6월 3일
편집: Salaheddin Hosseinzadeh 2015년 6월 3일

0 개 추천

Hi Alon,
As a C# programmer you know that "it fails" is not enough!
BTW, sometime when I want to generate a file specially on drive C, I have to open MATLAB as an administrator! You may wanna to this as well.

댓글 수: 4

Alon
Alon 2015년 6월 3일
It shows 0 elements in the array
Hello, I don't have much knowledge with matlab or c#.. Do I use this command right?
myFloatArray=fread(fid,1,'float',0,'l');
Dear Alon,
Just answer James questions.
When you open the file using fopen, do you get a valid File ID?
A valid file ID is an integer.
fileID = fopen('myfile.xyz')
Most importantly, pleaseee post the error message
Alon
Alon 2015년 6월 3일
편집: Alon 2015년 6월 3일
I do it that way:
fid = fopen('myRecordBin.bin', 'r');
fread(fid,inf,'float',4,'l');
fclose(fid);
Now, I can read it and matlab's workspace show 69222 elements and in the vector it shows: 69222x1 double.
But my original float array(and not double) in c# is twice: 138444 elements.

댓글을 달려면 로그인하십시오.

James Tursa
James Tursa 2015년 6월 3일

0 개 추천

Did you open the file first to obtain a valid fID? Are you getting an error message? (If so, please post the entire message). Are you getting garbage for myfloat? (Indicating possible endian problem, or maybe header data not accounted for, or data is not really float, etc.)

댓글 수: 10

Alon
Alon 2015년 6월 3일
Hello I know that I need to at liite endian format. But what do I have to write in the skip? 0?
James Tursa
James Tursa 2015년 6월 3일
Do you have the C# code that wrote the file? If so, please post the relevant parts that did the writing.
Hello, here is the c# code:
float[] samples32array = samples32.ToArray();
using(BinaryWriter writer = new BinaryWriter(File.Open("C:/Users/alon/Desktop/myRecordings/myRecordBin.bin", FileMode.Create)))
{
foreach (var value in samples32array)
{
writer.Write(value);
}
}
Alon
Alon 2015년 6월 3일
편집: Alon 2015년 6월 3일
Now, I can read it and matlab's workspace show 69222 elements and in the vector it shows: 69222x1 double.
But my original float array(and not double) in c# is twice: 138444 elements.
James Tursa
James Tursa 2015년 6월 3일
편집: James Tursa 2015년 6월 3일
Are the values correct for the elements you did read? If so, are you sure there are 138444 floats in the file? What is the file size? To keep the values read in from fread as single use '*float' instead of 'float'.
Alon
Alon 2015년 6월 3일
편집: Alon 2015년 6월 3일
Hello, the values are like in c# but placed in wrong index . Size of file is: 553772 bytes. ==> 138443 floats. I've tried to use *float but what does it give me?
James Tursa
James Tursa 2015년 6월 3일
편집: James Tursa 2015년 6월 3일
"... placed in wrong index ..."
Please elaborate on the above phrase. Do the numbers match exactly but in the wrong index? If so, give an example of the first few numbers on the C# side and their locations on the MATLAB side to detail explicitly what the indexing problem is you are seeing.
Using '*float' vs 'float' should not have changed any of the values, so your last comment doesn't make sense to me.
'float' --> Read the numbers as float and convert to duoble
'*float' --> Read the numbers as float and keep them that way (i.e., single)
Alon
Alon 2015년 6월 3일
Alon
Alon 2015년 6월 3일
I'm sorry it didn't change the values but still they are placed wrong.
Hello! Thanks for you pleasing answers. I got my problem, here we go:
fid = fopen('myRecordBin.bin', 'r');
myarray=fread(fid,inf,'*float',0,'l');
fclose(fid);

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

질문:

2015년 6월 3일

댓글:

2015년 6월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by