File Exchange

image thumbnail

Read ZEMAX Beam File (ZBF)

version 1.2 (2.81 KB) by

Reads a ZEMAX Beam File into Matlab, allowing presentation of physical optics simulation results.



View License

ZEMAX is an optical simulation code ( One of its features, Physical Optics Propagation, calculates the electric fields as they travel through the simulated optical system. The results can be saved in the ZEMAX Beam File (ZBF) format. It is useful to be able to read the ZBF file into Matlab so the electric field amplitudes and phases can be plotted in Matlab for display. Multiple simulation results can be easily compared in this way.

Since this code uses mex, the user will first need to compile part of the code. I only tested this on Windows, and I believe the code will only compile using gcc. Please install MinGW ( and GNUMex ( Then, go to Matlab and type:

mex read_zbf_mex.c

This will compile the mex part of the code. To run the code, type

[ x y Ex Ey ] = read_zbf( filename )

and replace filename with a string containing the filename of the ZBF file to be read. To plot the results, issue commands like:

pcolor(x,y,real(Ex)); shading flat

to plot just the real part of the x-component of the E-field amplitude in the ZBF file.

I am grateful to Bill Jones for sending me his make_zbf C-code which I modified to make this code. I believe both of our codes are based on an example file included in ZEMAX.

Comments and Ratings (8)

Sean Bryan

Hi Mohamed,

I'm actually not sure how Zemax normalizes the ZBF files... For my application, I just wanted a relative map, so the absolute power wasn't what I was after. Let me know if you figure out more about the normalization...sorry I'm not any help.



Dear Sean Bryan

thanks for the file but I found that there is a difference in the power obtained from this file and when zbf is opened in beam file viwer inside Zemax

Do you have reason for this

You are right, I can't because of the work.

Sean Bryan

Weird...maybe GNUMex is broken for Windows 7. Thanks for pointing that out, I'll keep it in mind for the future.

Post your native Matlab solution? :) Or does work not allow that...

thanks for replying. I needed this solution for my work. After all we solved this problem in native MATLAB by using description from ZEMAX support of ZBF file structure. We made it with only using binary file functions.

I use WIN 7 and after installation of MinGW and GNUMex the line mex read_zbf_mex.c didn't work. Also mex -setup doesn't recognize this compiler.

Sean Bryan

Did installing MinGW and GNUMex work for you?

Once those are installed, starting Matlab, changing into the directory where the code is, and typing "mex read_zbf_mex.c" should compile the code. Is that step failing even with MinGW and GNUMex installed?

Sean Bryan

(Oh, I do think it could be rewritten in native Matlab. I was trying to minimally change an existing c-code, so that's why I went with mex.)

The description of how to compile file is poor.
1. Please let more instruction for using.
2. Is it possible to write same function without using mex file (fopen, fread, ...)?



More careful definition of x and y coordinate matrices.


Changed the memory allocation on the output matrices to improve speed.

MATLAB Release
MATLAB 7.8 (R2009a)

Inspired: Read a binary ZEMAX beam file (ZBF)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today