Reading ZRD files with MATLAB

  • 26 October 2022
  • 8 replies

Hi team,


I want to be able to import Zemax ZRD binary files into MATLAB. I tried fopen and fread but had no luck. I see examples of code for reading Zemax ZBF files on the File Exchange but nothing for ZRD files.
Please help.


Best answer by Ray 27 October 2022, 11:52

View original

8 replies

Hi all!

Thank you for your support; I managed to use the batch ray database processing in Matlab. However, the intensity reading appears to be incorrect, as I expected to see the total intensity in microns based on the trends in the original files. Is there anything done to convert the values, or is there something I am missing at some point in the processing? Note that I used the sample Matlab file provided in the tutorial with some changes to support my own data.


Here is sample of output power!


Hi Sandrine,


Thank you for support, I got it fixed. 

Userlevel 6
Badge +2

Hi @Priscilla ! Sorry I never got back to you regarding your issue. Did you manage to resolve it? Have you tried in debug mode?

Hi Sandrine,


Thank you for your response. I attempted to use the same files from the link you provided without making any changes. In addition, as shown in the screenshot below, I kept all files in the same folder. However, when the script is run, no result is displayed. I'm working with both 64-bit MATLAB and OpticStudio software. What could be causing this?

Userlevel 6
Badge +2

Hi Priscilla

I ran the MATLAB_ZRDLoaderFull.m file from and it worked without any problem. In the code, the dll should be left in the same folder as the Matlab file:


Let us know if this is what you did.

Hi team,


Kindly assist with the above error; I am stuck so far that MATLAB seems not to understand some methods and functions in this ZRD file reading script. What to do to resolve the matter? 


Thank you all.


Hi Ray,


Thank you for your advice and support. I have used the sample batch ray reading .m file from the link you provided. I just changed the file name and directories in the code. However, I am getting the errors below; kindly assist on how to correct them.



Userlevel 4

Hi Priscilla,

Having written such an importer many years ago, I would not advise you to do it, at least not the way I did. Zemax 22.2 introduced a way to save the ZRD content as text tab separated value files. This makes huge files but the loading is much simpler.

This knowledge base article also offers a solution with the API and a DLL:

If you still think you need to write such importer in Matlab, you may start from the python code below:


A few additional points:

  • Only uncompressed full data ZRD can be read and the actual format is described in the manual. This is not difficult per se, but having it to be fast may require some work. You do not want to use one fread per value, you want to read massive blocks (the “precision” and “skip” arguments of fread are useful for that). You may want to first do a pass to determine the number of your rays and their blocks size. Another way that may be good is using memory mapping (never tried it, but it looks like it would solve most of speed issues in reading for a hard drive).
  • There is a section in the manual documenting the ZRD format that is called “Comments on Data Structure Memory Packing”. Reading it is very important for the importer to work.
  • Rays are saved as “tree” in the ZRD format, that is each branch that has split from one ray is part of the ray. If you want to do some processing of the individual branches (for example, only the rays that have ghosted on a given surface), you will need to first split the trees into individual “single” rays. That is the most complicated part.