File Exchange

image thumbnail

Imagic, MRC, DM and STAR file i/o

version (20.7 KB) by Fred Sigworth
Functions for reading and writing image files and STAR files for electron microscopy


Updated 06 Jan 2019

View License

This directory contains m-functions for reading and writing files used in electron microscopy and 3D reconstruction. The file formats those used by the IMAGIC software package (Image Science GmbH; EMAN and Frealign are public-domain programs that also use this format), the MRC program library, and the Digital Micrograph (Gatan, Inc.) file format. These functions were written on what published information we could find, and work for our limited purposes.

The functions generally assume that little-endian files are being read by a little-endian machine. However, some functions might still work with big-endian machines such as PowerPC, as they were originally written on a Mac computer.

Fred Sigworth, Liguo Wang
Yale University

Loads part or all of the data in an Imagic file pair into a matlab 3-d array. This function also returns a data structure with part of the information from the header.

Writes an entire matlab 3-d array as an Imagic file with float32 data type.

Create, read or write a datastructure that contains all the information in an Imagic header. The structure (a struct of arrays) allows manipulation of header information. For writing very large datafiles, the header file can be constructed and written out separately by these functions.

ReadDMFile (replaces ReadDM3)
Read a file generated by Digital Micrograph, version 3 or 4. These files have a large tree structure, and the code inside this function can be modified to return any field or fields of this structure. At present just the pixel size and number of pixels is returned along with the raw image.

Read part or all of an MRC-format file (2d or 3d) into a Matlab array; also retrieves information about the image size.

Write an entire 3d Matlab array into an MRC-format file

Write the header of an MRC-format file, allowing the user to write the rest of the file directly with the fwrite function.

Reads a STAR file, for example generated by Relion, into a cell array of block names, and a cell array of structures containing the block data.

Writes a MATLAB struct to a STAR file.

Cite As

Fred Sigworth (2020). Imagic, MRC, DM and STAR file i/o (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (21)

Jonas Funke

Dear Fred, I tried the new version of ReadMRC with Matlab2018b on the new MacMini (2018) and it works. Thanks a lot.

Dear Jonas, perhaps the new version I just uploaded will fix your problem. I had a similar problem when using a computer in Japan, where the size of a char (in Unicode) is not always one byte! I think it's fixed now.

Hi, Yin,
My email is
I normally display images by the following, which places the origin in the lower left.
axis xy;
You can also rotate and flip the image, e.g.
imagesc(rot90(m,3)); % display it after rotating 3x 90 degrees
imagesc(flipud(m)); % flip the image.

yin zhen

Dear professor Fred ,i have tried many times ,but the image that displayed by your code is different from EMAN2 or IMOD,i dont know whether i am right ,can i connnect you by e-mail ?by the way ,can it translate MRC into other format image?

yin zhen

Dear Yin, you can write a script that calls our functions, or you can call them from the command line. For example to display an image you can write

yin zhen

thanks for your code ,Should I write a main function to call the function in your folder,or how can i do if i just want to read a MRC format image

Jonas Funke

Hi, thanks for the great scripts. I am using the ReadMRC frequently, but unfortunately, it stopped working since I upgraded from Matlab 2016b to Matlab2018b. It throws an "ReadMRC: not enough data in file." error message. It looks like the "cnt" value (from [map,cnt]=fread(f,ndata,string); ) is smaller thant the "ndata". Is there a way to fix this?

A delayed answer to Alex Bard from last year: I had a similar problem once with a workstation in Japan. I had assumed that one character occupies one byte, but this is wrong when extended character sets are used. I think I've fixed that bug, but please check.

I might be a year too late but if anyone uses IMOD to generate MRC files, you might want to know that since Sep 2017 they made a subtle update of storing the values as signed integers (-128 to 127) instead of unsigned (0 - 255), creating images having its dynamic equilibrium shifting halfway across the spectrum circularly. I would, however, suggest users write a separate function to convert the read images back to its original distribution, instead of changing Sigworth's algorithm altogether as the old MRC files are still stored as unsigned integers. Or, you may use some extra command line input to specify the output as unsigned.

Here's the quote from IMOD's user guide Section 2.1

"In IMOD 4.9, programs write MRC files in byte mode as signed by default (values of -128 to 127), which is a change from previous behavior to conform with a clarification of the MRC standards. Such files will not be read correctly by IMOD versions before 4.3, and you may encounter other software that has difficulty with signed byte files. Two programs have command-line options to produce unsigned output files (values 0-255): newstack (option "-byte 0") and clip (option "-m ubyte"). In addition, you can make all programs write unsigned bytes by setting the environment variable WRITE_MODE0_SIGNED to 0. As of September 2017, ImageJ does not read signed byte files correctly unless you import them through the Bioformats tool. Please inform the IMOD developers if you run into other up-to-date software that does not read signed bytes correctly."

Please note that the ReadDM3 function has been updated to read dm4 files as well. See this entry:

Alex Bard

The ReadMRC function works perfectly on my local machine, using Matlab R2017b on Win64. However, when I move the exact same code onto the university's supercomputer, which uses R2015b for 64-bit Linux, I get an error message that says, "ReadMRC: not enough data in file." Anyone have any idea what I need to do to fix that? As far as I can tell it's able to open or at least see the file (because trying to read a nonexistent file gives a different error message), but for some reason it's not able to access the data or something.

Thanks for the function! However, whenever i try to open my mrc file using


I get the error "Undefined variable "sample" or class "sample.mrc"
I am using version R2017a on Linux

now i am trying to use ReadMRC. For example, my data is data1 so when i try to ReadMRC(data1), it does not work T.T

What is this problem?


Thanks for sharing! Small bug with ReadMRC when reading one image at a time, it will load the full MRC with the first image since nz=min(,numSlices); is only redifined in the if startSlice>1

Oh yes, I wrote it and use it on Mac OS X. --Fred


Is this compatible with Matlab for Mac OS X?

Thanks, Miriam


Liang Jin

Great modules for reading/writing MRC files.


ReadDMFile replaces ReadDM3, allowing DM4 files to be read as well. New functions for reading and writing STAR files. Some bugs fixed.

MATLAB Release Compatibility
Created with R2010a
Compatible with R2010a to R2018b
Platform Compatibility
Windows macOS Linux

Inspired: Read .dm3 and .dm4 image files