Main Content

fitswrite

Write image to FITS file

Description

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 arguments. For example, Compression="rice" specifies the Rice compression algorithm.

example

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 the red channel of the image.

X = imread("ngc6543a.jpg");
R = X(:,:,1);
imshow(imtile({X R}))

Figure contains an axes object. The hidden axes object contains an object of type image.

Write the red channel data to a FITS file.

fitswrite(R,"myfile.fits");

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 the red, green, and blue channels of the image.

X = imread("ngc6543a.jpg");
R = X(:,:,1);
G = X(:,:,2);
B = X(:,:,3);
imshow(imtile({X R G B},GridSize=[1 NaN]))

Figure contains an axes object. The hidden axes object contains an object of type image.

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")

Input Arguments

collapse all

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

Name of FITS file, specified as a string scalar or character vector.

  • To write to the current folder, specify the name of the file in filename.

  • To write to a folder different from the current folder, specify the full or relative path of the file in filename.

Example: "myFile.fits"

Example: "C:\myFolder\myFile.fits"

Example: "myFolder\myFile.fits"

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.

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

Writing mode, specified as a string scalar or character vector. 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 these 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