File Exchange

image thumbnail

DM3 Import for Gatan Digital Micrograph

version (1.96 MB) by Robert McLeod
Imports Gatan .DM3 format files with tags (images, spectra, & spectral images) into a MATLAB struct.


Updated 08 Jan 2013

View License

This script acts to import files from Gatan's .DM3 file format, utilized for electron microscopy, into a MATLAB structure. The fields of the MATLAB structure can then be referenced with the dot-operator.

For example, one can load and display a file with the appropriate scaled axes in nanometers and electrons with the following example script:

dm3struct = DM3Import( 'RandomBrightfieldImage.dm3' );
N = length( dm3struct.image_data );
imagesc( (1:N).*dmstruct.xaxis.scale (1:N).*dmstruct.yaxis.scale, ...
dm3struct.image_data.*dm3struct.intensity.scale );

This script currently imports images, EELS spectra, and spectral images. Now imports annotations (text) written on the image as well.

This script was constructed largely by parsing example DM3 files with a hex editor. It uses regular expressions to find the tags that indicate the start points of the various fields within the files, and then strips the data. To the best of my knowledge there is not existing documentation on the actual object used to write DM3 format files so it is not practical to output to DM3 format.

Comments and Ratings (11)

shen ruohan

Excellent! Thank you very much!

Hi Allen, my email is in the comments at the top of the file. Feel free to email me a zip of an example along with a list of what you need. I assume this is for spectral imaging EELS?


I'd love for this to import spectra from monochromators. It almost accomplishes this, but misses key parameters for starting frequency, etc. (let alone the other available fields such as grating, dwell time etc.) I can send example .dm3 files if that would be helpful/interesting. Thanks again for the submission!


Hi Robert
Thanks for that, its really appreciated


It seems somewhere in the file, there is a tag that indicates which direction the scan is rotated. Same goes for image rotation. However, I do not know where it is so some microscopes the axis are correct and others are not. The fastest way to change the axes is to use the built-in permute function, which can reduce your code to one-line:

spec_temp = permute( spec_temp, [2 1 3])

for example...


I think you need to download the full .zip file with the example .dm3 files.


Hi there, just found a bug in loading EELS spectral image.
It seems to be reading to the matrix in the wrong order.

A work around is below
si_struct = DM3Import( FILENAME );
spec_temp = si_struct.image_data .* si_struct.intensity.scale;
[M, N, px]=size(spec_temp);


for J = 1:N
for I = 1:M

File1(t,p,:)=spec_temp( I, J, : );
if J==N && I==M
elseif p>N


Hi Mr.Robert;

I tried this function but it gave this Error

Warning: No such file or directory
> In DM3Import at 68
In example_dm3script at 33
Attempt to reference field of non-structure array.

Error in example_dm3script (line 34)
disp( hitachi_struct.image_text )
Have you any idea?
Thank you I'm waiting for your answer Mr.


Hi Robert!
I cannot use the script to import complex dm3 images... The error message is following:
"Error using reshape
To RESHAPE the number of elements must not change.

Error in DM3Import (line 232)
dm3struct.image_data = reshape( readTagData( dataloc(index) ), xdim, ydim );"
Do you have any suggestions how to fix that?



Excellent submission - ImageJ recently displays a blank image for DM3 format. Not so with this m-file- import works like a charm.


Added examples of using DM3Import with a spectral image, and fixed some bugs relating to DM3 files from other instruments.

Added example of using a spectral image and fixed a couple of bugs related to DM3 files from other instruments.

Fixed bugs in the import of spectral images that had an interlacing problem.

Added support for importing microscope state in the form of tags for Hitachi TEMs.

Added support to import annotations written on the screen.

Added support for HAADF images (i.e. datatype '11' in GATAN's scheme) since a user asked for it. If anyone finds other bugs please e-mail me and I will fix them.

Update 02Dec2010: Now with examples, and some changes to import separate xaxis for individual spectra.

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: Autoscaleit