This article describes:^{ }

- How to set up a communication link between Zemax and MATLAB®
- How to use the Zemax DDE Toolbox for MATLAB
- Common problems and how to solve them

Authored By: Derek Griffith

Published On: January 3, 2006

**Zemax and MATLAB : A Powerful Pair**

Zemax features a built-in DDE (Dynamic Data Exchange) server that allows other Windows^{®} applications access to Zemax functions. Programming DDE applications can be somewhat complex if you are working in a compiled language such as C++. An alternative is to use one of the scripting languages such as Tcl, Python or MATLAB. MATLAB is a great choice since it is much more than a scripting language. MATLAB provides a wealth of functions for processing and visualizing scientific and engineering data and there are toolboxes for MATLAB that extend the basic capabilities into a wide variety of more specialized areas and disciplines.

**What Is DDE?**

DDE is a facility and a protocol provided by Windows to allow applications to communicate.
There are a few modes of DDE interaction, the simplest being the exchange of text strings of data. One of the applications must set itself up as a server. Zemax is the DDE server in this case, so you don't have to do anything except start Zemax. Once you have started Zemax, the DDE server is ready and waiting for a client to connect and make requests for services. The client application in this case is MATLAB.

**Accessing Zemax from MATLAB**

If you look in the Zemax manual in the chapter on "Zemax Extensions" in the section called "The Data Items", you will see the various request strings you can send to Zemax from MATLAB (or from other DDE clients). The manual also documents the reply that Zemax will send back to the client application. The requests can be for Zemax to perform a particular function or computation, or simply to send back data on the lens currently stored in the Zemax DDE server.

Now here is an important point to note. Zemax always maintains a separate copy of the lens data in the DDE server memory to that available to the user in the Zemax Lens Data Editor (LDE). This is a very good policy, since you don't want some DDE client coming in unexpectedly and altering your lens data. There are functions that explicitly copy the lens in the LDE to the DDE server or the other way around. The PushLens item instructs Zemax to copy the lens data in the Zemax DDE server into the LDE, and the GetRefresh item will copy the lens data in the LDE into the DDE server. As you develop your application in MATLAB, it is important to keep track of your data and take care how you use the PushLens and GetRefresh items.

From the Zemax Preferences dialog, you have control over whether extensions (MATLAB in this case) are allowed to tell Zemax to push lens data from the DDE server into the LDE. Go to the Editors tab on the Preferences dialog and look for a check box labeled "Allow Extensions to Push Lenses".

MATLAB has a few basic functions for performing DDE client operations. They include the following.

ddeinit - This function is used to initiate a DDE conversation

ddereq - Used to request data from the DDE server

ddeterm - Terminates the DDE link

The basic sequence for accessing Zemax from MATLAB is

1) Start Zemax and MATLAB.

2) Initiate the DDE link from MATLAB using the ddeinit function.

3) Send requests and obtain replies from Zemax using the ddereq function.

4) Terminate the link to Zemax using the ddeterm function.

The ddereq function is used in the text mode, that is, you send data to Zemax in the form of a text string and the answer is returned in the form of another string. To get the strings into object classes that are more useful in MATLAB (floats, doubles, integers etc.) you will use the sscanf function or other functions for string data conversion e.g. str2num.

**The Zemax DDE Toolbox for MATLAB**

You can save yourself some effort by downloading the Zemax DDE toolbox. This toolbox is open source under the GPL and implements a MATLAB function for each of the data items documented in the Zemax manual in the chapter on Zemax Extensions. The corresponding MATLAB function in the toolbox has the same name as the data item, but prefixed with "z". To get started, perform the following steps.
- Download the toolbox archive (.zip file).
- Unzip the archive to a sub-directory called MZDDE anywhere on your local disk.
- Start Zemax and MATLAB.
- Add the directory MZDDE to your MATLAB path to enable MATLAB to find the new functions (go to the File menu and select Set Path ...)
- If you type help mzdde at the MATLAB prompt, you should get a list of all the functions in the toolbox and a short description of each function. If so, you are ready to use the toolbox.
- Use the function zDDEInit to open the DDE link to Zemax. This step is important. If you don't do this before trying to execute other calls in the toolbox, MATLAB will issue a verbose error message suggesting that something terrible has happened and that you should contact the MathWorks technical support staff. If this happens, just ignore the error and make the call to zDDEInit.
- Call other functions in the toolbox as required, making judicious use of zPushLens and zGetRefresh if you need to interact with the Zemax user interface. Of course, you can call the functions from the MATLAB command line, or incorporate them into your MATLAB functions and scripts.
- Finish off by calling zDDEClose. This is not important in most cases, and you can just terminate MATLAB and Zemax without closing the DDE link.

Note that the toolbox function names are capitalized for readability. MATLAB function names are case insensitive under Windows?, so that zddeinit is equivalent to zDDEInit. This is convenient at the MATLAB command line, but the capitalization helps for readability in functions and scripts. The source code for all of the functions is available for modification and reuse, and you can examine the source to see how the basic MATLAB DDE calls are used.

**Timeouts**

Every DDE call made by MATLAB to Zemax is subject to a timeout limit. The default is set in the zDDEInit function to 3 seconds. That is, if Zemax does not respond to the DDE request within 3 seconds, the MATLAB call will return empty-handed. You will want to increase the timeout limit if you are calling on Zemax to do time-consuming computations. This can be done using the zSetTimeout function call.

**Example**

Here is a simple example of an interaction with Zemax at the MATLAB prompt. Having started Zemax in the usual way, enter the following commands in MATLAB.

**>> zddeinit**

ans =

0

**>> zLoadFile('C:/Zemax/Samples/Sequential/Objectives/Cooke 40 degree field.zmx')**

ans =

0

**>> zpushlens(1)**

ans =

0

**>> [x, y] = SpiralSpot(0.4, 0, 1, 10, 10000);**

>> plot(x,y)

You should get a plot something like this ...

Firstly the DDE link was initialized and the result of 0 indicated success, then Zemax was asked to load a lens into the DDE server with the zLoadFile function call. Now, a pretty way of representing a spot diagram is to trace a spiral of rays through the lens pupil and to plot the ray intercepts at the image plane. This is implemented in a toolbox function called SpiralSpot. Get help for the function by typing help spiralspot at the MATLAB prompt. Because SpiralSpot acts on the lens in the LDE by starting out with a zGetRefresh, it is necessary to perform a zPushLens before calling SpiralSpot. A timeout of 1 second was specified for zPushLens to complete in this case.

Note that in this example you would get the same result if you had loaded the same lens into Zemax from the Zemax File menu rather than using the zLoadFile call.

**Tracing Large Numbers of Rays**

The standard text mode of the DDE call is adequate for many purposes, but not when large amounts of data have to be exchanged between the client and the server. This situation arises when you need to trace a large number of rays.

Instead of tracing the rays one at a time, an array containing a list of all the rays to be traced is created, and then the entire array is passed to Zemax at one time. Zemax then traces all the rays, and passes the entire array back to the client. Rather than transferring all this data over DDE, a pointer to the location in memory of the array is passed instead. This eliminates the bottlenexck of DDE communication when tracing large numbers of rays.

Unfortunately, at the time of writing the toolbox, MATLAB did not allow DDE modes other than text. To circumvent this problem, it was necessary to write some C code in mex (MATLAB executable) format. The resulting .dll is called zArrayTrace.
Type help zArrayTrace to get more information on bulk ray tracing. It will also be useful to consult the corresponding section in the Zemax manual. To help set up the ray input data for zArrayTrace mode 0, there is a function called genRayDataMode0.

**Compiling Standalone Zemax Extensions written in MATLAB**

If you have the MATLAB compiler, it is possible to compile your MATLAB scripts written using the toolbox to standalone extensions that can be run from the Zemax Extensions menu. There are a few traps that must be avoided when attempting this.

Firstly, there are a few MATLAB functions that cannot be compiled, and you will have to think of workarounds for such functions (fortunately not many).

Secondly, while your standalone extension should run fine on a machine on which MATLAB is installed, it will not run on a machine that does not have the MATLAB runtime libraries. You must therefore package your extension together with the MATLAB runtime libraries to make it fully portable. MathWorks makes this easier by packaging most of the required runtime libraries in a self-extracting archive called mglinstaller.exe (typically found in C:/MATLAB/extern/lib/win32 or thereabouts).

The final trap is that the regular MATLAB runtime libraries do not contain the code for the DDE functions mentioned above. These .dll files can be found by searching your MATLAB installation for files matching dde*.dll (typically C:/MATLAB/Toolbox/matlab/winfun or thereabouts). The runtime libraries, including the DDE libraries, must be installed to a directory that is on the Windows search path.

**Things to Remember when using the Toolbox**

1) Your first call after starting up Zemax and MATLAB must be to zDDEInit.

2) Timeouts can be a problem. Increase the DDE timeout limit using zSetTimeout if necessary.

3) Zemax has two copies of the lens data, one displayed in the Lens Data Editor, and the other stored in the DDE server. Your can copy the one to the other using the zPushLens and zGetRefresh functions. zPushLens always requires a single parameter giving the timeout in seconds.

4) zPushLens will only work if you have granted Zemax extensions permission to push lenses by checking the box on the Editors tab of the Zemax Preferences dialog.