Main Content

Read FITS image data


imgdata = readImg(fptr)
imgdata = readImg(fptr,fpixel,lpixel)
imgdata = readImg(fptr,fpixel,lpixel,inc)


imgdata = readImg(fptr) reads the entire current image. The number of rows in imgdata will correspond to the value of the NAXIS2 keyword, while the number of columns will correspond to the value of the NAXIS1 keyword. Any further dimensions of imgdata will correspond to NAXIS3, NAXIS4, and so on.

imgdata = readImg(fptr,fpixel,lpixel) reads the subimage defined by pixel coordinates fpixel and lpixel. The fpixel argument is the coordinate of the first pixel and lpixel is the coordinate of the last pixel. fpixel and lpixel are one-based.

imgdata = readImg(fptr,fpixel,lpixel,inc) reads the subimage defined by fpixel, lpixel, and inc. The inc argument denotes the inter- element spacing along each extent.

This function corresponds to the fits_read_subset (ffgsv) function in the CFITSIO library C API.


Read an entire image.

fptr = fits.openFile('tst0012.fits');
data = fits.readImg(fptr);

Read a 70x80 image subset.

fptr = fits.openFile('tst0012.fits');
img = fits.readImg(fptr,[11 11],[80 90]);


  • MATLAB® reads FITS image data in the order that it appears in the file, but some software packages for reading and writing FITS image data assume that the image data is stored in an order in which the bottom row of the image is first. Consequently, FITS image data displayed in MATLAB may appear flipped in the up-down direction (that is, about a horizontal axis) when compared to the same data displayed using other software packages. To flip an image in MATLAB, you can use the flipud function on the output of before displaying the image.