Main Content

matlab.io.fits.writeImg

Write FITS image data

Syntax

matlab.io.fits.writeImg(fptr,data)
matlab.io.fits.writeImg(fptr,data,fpixel)

Description

matlab.io.fits.writeImg(fptr,data) writes image data to a FITS file. The number of rows and columns in data must respectively match the values of the NAXIS2 and NAXIS1 keywords of the FITS file. Further dimensions of data must match the values of the keywords NAXIS3, NAXIS4, and so on.

matlab.io.fits.writeImg(fptr,data,fpixel) writes image data to a subset of a FITS image. The fpixel argument is the coordinate of the first pixel in the FITS image region.

Examples

collapse all

Create a new FITS file and add a 256-by-512 image to it. Then write an image to the file.

import matlab.io.*
fptr = fits.createFile("myfile.fits");
fits.createImg(fptr,"long_img",[256 512])
data = reshape(1:256*512,[256 512]);
data = int32(data);
fits.writeImg(fptr,data)
fits.closeFile(fptr)

Examine the file metadata and then delete the file.

fitsdisp("myfile.fits")
HDU:  1 (Primary HDU)
	SIMPLE  =                    T / file does conform to FITS standard
	BITPIX  =                   32 / number of bits per data pixel
	NAXIS   =                    2 / number of data axes
	NAXIS1  =                  512 / length of data axis 1
	NAXIS2  =                  256 / length of data axis 2
	EXTEND  =                    T / FITS dataset may contain extensions
	COMMENT   FITS (Flexible Image Transport System) format is defined in 'Astronomy
	COMMENT   and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H
delete myfile.fits

Create an 8-by-12 image in a FITS file, and then write data to the portion of the image inside a 2-by-3 border.

import matlab.io.*
fptr = fits.createFile("myfile.fits");
fits.createImg(fptr,"uint8",[8 12])
data = ones(4,6);
fits.writeImg(fptr,data,[3 4])
fits.closeFile(fptr)

Examine the image data and then delete the file.

fitsread("myfile.fits")
ans = 8×12

     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     1     1     1     1     1     1     0     0     0
     0     0     0     1     1     1     1     1     1     0     0     0
     0     0     0     1     1     1     1     1     1     0     0     0
     0     0     0     1     1     1     1     1     1     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0

delete myfile.fits

Tips

  • This function corresponds to the fits_write_subset (ffpss) function in the CFITSIO library C API.

  • To use this function, you must be familiar with the CFITSIO C interface. You can access the CFITSIO documentation at the CFITSIO website.

  • MATLAB® writes raw FITS image data in the order given, 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 written by MATLAB may appear flipped in the up-down direction (that is, about a horizontal axis) when displayed using other software packages. To flip an image in MATLAB, you can use the flipud function on the image data before writing the image with matlab.io.fits.writeImg.

Extended Capabilities

expand all

Version History

expand all