can anybody help in understanding fopen and fread?

As per my understanding, fopen open the file in a mode which we want. For example, if we say r,then it opens in read mode. fread, read the open file and returns the no of elements.
Based on above line, i tried below code.
f= fopen('train-images-idx3-ubyte', 'r')
Ans:
f =
56
Here what 56 represents? I understand it is file identifier. How it is generated?
[a, count]= fread(f,4,'int 32')
a =
1.0e+09 *
0.0509
1.6259
0.4698
0.4698
count =
4
Could anybody help me to understand the above result.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 20 Jun. 2014

1 Stimme

f is some identification number of the file. Don't worry about what it's actual value is or how it's generated - it doesn't matter (as long as it's not -1 which indicates an error). It's simply used to tell fread() or fprintf() what file to talk to.
The fread line read four 4-byte integers from the binary file. You store them into variable "a". It should be 'int32' rather than 'int 32' though.

11 Kommentare

subha
subha am 20 Jun. 2014
what about count ? is that represents no of columns?
Image Analyst
Image Analyst am 20 Jun. 2014
Bearbeitet: Image Analyst am 20 Jun. 2014
It's the number of values read, which is 4 because that's how many you told it to read. You can pass in an array, such as [2,2] instead of 4 if you want the results in a 2 by 2 matrix. It could be less than 4 if, for example, you were only 1, 2, or 3 numbers from the end of the file so that is all it could possibly return.
subha
subha am 26 Jun. 2014
Bearbeitet: subha am 26 Jun. 2014
I am reading an image file. what a value represents? Is it intensity of pixel?
Yes, usually. Sometimes though a pixel can represent a distance or height or something else other than reflectance or brightness.
subha
subha am 26 Jun. 2014
How can i understand, in my case what it represents
You have to know about how and why your image was taken. Was it a fluorescence micrograph where color indicated what marker tagged what material? Was it a profilometer image where the value is the height of your sample above the table top? Was it just some vacation snapshot where the pixel is just the color of some location in your scene? Surely, you MUST know something about your images. Otherwise why are you even doing anything with them?
subha
subha am 29 Jun. 2014
Bearbeitet: subha am 30 Jun. 2014
My image file consists of handwritten digits. It is grey scale image. In my image file, it is mentioned, pixels values are in 0-255. 0 represent background(white), 255 represent foreground(black). So it means, i can say my values represents color of scene. since, it is a grey level image. it is representing intensity of an image. Is that sensible?
Why is it not in some standard image file format??? If it is in a standard format for images, you can use imread() rather than build your own file reader with fread(). I mean, why make it hard on yourself?
subha
subha am 30 Jun. 2014
Bearbeitet: subha am 30 Jun. 2014
The image is taken from this website. http://yann.lecun.com/exdb/mnist/. i was not able to predict image format. They are not in standard image format. if u know any idea suggest me.
subha
subha am 30 Jun. 2014
Thanks a lot. it really helps me..

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Marta Salas
Marta Salas am 20 Jun. 2014
The file identifier is a integer greater than 3. Identifier -1 means fopen can not open the file, and MATLAB reserves file identifiers 0, 1, and 2 for standard input, standard output (the screen), and standard error, respectively.
[a, count]= fread(f,4,'int 32')
This line is reading 4 values in the file f such that the source data is class uint32. Then the 4 values are stored in a and the number of values read from the file is stored in count

Kategorien

Mehr zu Data Import and Analysis finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 20 Jun. 2014

Kommentiert:

am 30 Jun. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by