View License

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

» Watch video

Highlights from
Read ZEMAX Beam File (ZBF)

Be the first to rate this file! 7 Downloads (last 30 days) File Size: 2.81 KB File ID: #32099 Version: 1.2

Read ZEMAX Beam File (ZBF)



06 Jul 2011 (Updated )

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

| Watch this File

File Information

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.


This file inspired Read A Binary Zemax Beam File (Zbf).

MATLAB release MATLAB 7.8 (R2009a)
Other requirements This was tested in Windows. I believe the code will only compile using gcc, so please install MinGW ( and GNUMex ( to compile the mex file.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
29 Aug 2011 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.


Comment only
28 Aug 2011 Mohamed

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

Comment only
04 Aug 2011 Alexander Brodsky

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

Comment only
02 Aug 2011 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...

Comment only
30 Jul 2011 Alexander Brodsky

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.

Comment only
29 Jul 2011 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?

Comment only
29 Jul 2011 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.)

Comment only
24 Jul 2011 Alexander Brodsky

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, ...)?

Comment only
06 Jul 2011 1.1

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

22 Jul 2011 1.2

More careful definition of x and y coordinate matrices.

Contact us