Main Content

fitswrite

Write image to FITS file

Description

example

fitswrite(imagedata,filename) writes the specified image data to the Flexible Image Transport System (FITS) file specified by filename. If the file exists, fitswrite overwrites it.

example

fitswrite(imagedata,filename,Name,Value) creates a file with additional options using one or more Name-Value pair arguments. For example, 'Compression','rice' specifies the Rice compression algorithm.

Examples

collapse all

Create a FITS file containing the red channel of an RGB image. The red channel will be represented by grayscale in the final image.

Read a sample image and isolate its red channel.

X = imread('ngc6543a.jpg');
R = X(:,:,1);

Write the red channel data to a FITS file.

fitswrite(R,'myfile.fits');

Display the FITS metadata.

fitsdisp('myfile.fits');
HDU:  1 (Primary HDU)
	SIMPLE  =                    T / file does conform to FITS standard
	BITPIX  =                    8 / number of bits per data pixel
	NAXIS   =                    2 / number of data axes
	NAXIS1  =                  600 / length of data axis 1
	NAXIS2  =                  650 / 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

Create a compressed FITS file with three images constructed from the channels of an RGB image. First create the file with one channel, then append the file with the other two.

Read a sample image and isolate its red, green, and blue channels.

X = imread('ngc6543a.jpg');
R = X(:,:,1);
G = X(:,:,2);
B = X(:,:,3);

Write red channel data to a FITS file using Rice compression.

fitswrite(R,'myfile.fits','Compression','rice');

Append the green and blue channel data to the FITS file using Rice compression.

fitswrite(G,'myfile.fits','WriteMode','append','Compression','rice');
fitswrite(B,'myfile.fits','WriteMode','append','Compression','rice');

Display the FITS file.

fitsdisp('myfile.fits');
HDU:  1 (Primary HDU)
	SIMPLE  =                    T / file does conform to FITS standard
	BITPIX  =                   16 / number of bits per data pixel
	NAXIS   =                    0 / number of data axes
	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

HDU:  2 
	XTENSION= 'BINTABLE'           / binary table extension
	BITPIX  =                    8 / 8-bit bytes
	NAXIS   =                    2 / 2-dimensional binary table
	NAXIS1  =                    8 / width of table in bytes
	NAXIS2  =                  650 / number of rows in table
	PCOUNT  =               101902 / size of special data area
	GCOUNT  =                    1 / one data group (required keyword)
	TFIELDS =                    1 / number of fields in each row
	TTYPE1  = 'COMPRESSED_DATA'    / label for field   1
	TFORM1  = '1PB(470)'           / data format of field: variable length array

HDU:  3 
	XTENSION= 'BINTABLE'           / binary table extension
	BITPIX  =                    8 / 8-bit bytes
	NAXIS   =                    2 / 2-dimensional binary table
	NAXIS1  =                    8 / width of table in bytes
	NAXIS2  =                  650 / number of rows in table
	PCOUNT  =                95976 / size of special data area
	GCOUNT  =                    1 / one data group (required keyword)
	TFIELDS =                    1 / number of fields in each row
	TTYPE1  = 'COMPRESSED_DATA'    / label for field   1
	TFORM1  = '1PB(470)'           / data format of field: variable length array

HDU:  4 
	XTENSION= 'BINTABLE'           / binary table extension
	BITPIX  =                    8 / 8-bit bytes
	NAXIS   =                    2 / 2-dimensional binary table
	NAXIS1  =                    8 / width of table in bytes
	NAXIS2  =                  650 / number of rows in table
	PCOUNT  =                96255 / size of special data area
	GCOUNT  =                    1 / one data group (required keyword)
	TFIELDS =                    1 / number of fields in each row
	TTYPE1  = 'COMPRESSED_DATA'    / label for field   1
	TFORM1  = '1PB(470)'           / data format of field: variable length array

Input Arguments

collapse all

Input image data, specified as a one-dimensional or multi-dimensional array.

File name, specified as a character vector or string scalar.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: fitswrite(R,'myfile.fits','WriteMode','append')

Writing mode, specified as a character vector or a string scalar. If the file specified already exists, the writing mode determines the behavior of fitswrite.

Writing Mode

Description

'overwrite'

Overwrite the existing file. This is the default behavior

'append'

Append the image data to the existing file.

If the file does not exist, then fitswrite creates a new file regardless of writing mode.

Compression algorithm to be used when writing a FITS image, specified as one of the following values.

Values

Description

'none'

No compression. This is the default behavior.

'gzip'

Compress image data as a GNU ZIP file.

'rice'

Compress image data using the Rice algorithm.

'hcompress'

Compress image data using the HCOMPRESS algorithm.

'plio'Compress image data using the PLIO algorithm.

Tips

  • 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 fitswrite.

Version History

Introduced in R2012a