MatLab can be a useful tool for image processing, but it can be useful to then take the results back into the Digital Micrographs DM) software from Gatan. Let’s use a landscape crop of the Lenna image with different height and width to demonstrate how to do this. This is what the variable looks like inside MatLab:

We start by plotting the result inside MatLab, in greyscale with the axis ticks hidden:

```
imagesc(Lenna_landscape)
axis image
colormap gray
set(gca,'xtick',[],'ytick',[])
```

This is the result:

Next, we will write this data to a binary file (*.bin) that is compatible with DM. The code to do this depends on the type of computer you have, 32 or 64 bit:

```
computer_type = computer ;
if strcmp(computer_type,'PCWIN') == 1
fid = fopen('Lenna_binary.bin','w');
fwrite(fid, Lenna_landscape , '*float32', 'l');
fclose(fid);
elseif strcmp(computer_type,'PCWIN64') == 1
fid = fopen('Lenna_binary.bin','w');
fwrite(fid, Lenna_landscape , '*float32', 'a');
fclose(fid);
end
```

This will write to the current folder a file ‘Lenna_binary.bin’. Then from within DM, go to File > Import and select this file. You may need to click to show ‘All Files’ in the import window to be able to see the *.bin file. In the dialogue box that opens you will need to select the format to ‘Binary’ and the data type to ‘Real 4 Byte’ to match the file we wrote earlier. Next enter the width and height that matches the dimensions shown in the MatLab workspace. The ‘width’ should equal the first MatLab dimension. This yields the following:

## Fixing the Transpose Problem

The result above has the Lenna image mirrored and rotated by 90°. This comes from the way MatLab treats data as ‘row-preference’, whereas DM, is ‘column-wise’. To fix this we simply transpose the data before writing the *.bin file. This is easy and just requires one extra character in the code:

```
computer_type = computer ;
if strcmp(computer_type,'PCWIN') == 1
fid = fopen('Lenna_binary.bin','w');
fwrite(fid, Lenna_landscape' , '*float32', 'l');
fclose(fid);
elseif strcmp(computer_type,'PCWIN64') == 1
fid = fopen('Lenna_binary.bin','w');
fwrite(fid, Lenna_landscape' , '*float32', 'a');
fclose(fid);
end
```

Next we can import this to DM just as before, but importantly we must also change the width and height dimensions around. Nothing else needs to be changed.

Now the results match the MatLab view as expected.

## Importing 3D Data (stacks / series)

If the MatLab variable is a stack or image-series, then the procedure is exactly the same, only you must also specify the ‘Depth’ of the data during import. This must match the number of frames – i.e. the third dimension in MatLab.

hi

very nice

What good education provided in MATLAB

Is really useful

Thanks

very good

Hi Lewys, the following DM script from Vincent Hou might be a different (and more comfortable) way of linking MAT and DM files:

@ DM-ScriptingDatabase.

(I haven’t tested that one, but I have written my own Matlab import script in the past a well, and it’s still lingering on my harddrive somewhere…)

Hi Bernhard, thanks for the tip. The transpose ‘feature’ is a minor annoyance. I’ll check out Vincent’s script, but this longhand way should be independent of DM version. Does his work with GMS 2.3? Would be great if DM could support this natively 🙂

Hi Lewys and Berhard,

My old script for MatLab file exchange should still work with GM 2.x.

Cheers,

Vincent