Soon, we will launch a new and updated customer portal, which is an important step toward providing our customers with one place to learn, interact, and get help.
Learn more.

How to Ensure MATLAB Will Read Your Text File

This article explains what to do if MATLAB will not read your text file.
Liz Gerrish and Alissa Wersal
08/26/2015
Frequently Asked Questions

Introduction

In 2010, the default text encoding in Zemax OpticStudio was changed from ANSI to Unicode to support multiple language interfaces. Unicode is considered an industry standard for representation of world wide text sets. While most applications can easily process Unicode-encoded text files, some applications, such as MATLAB, require the text file to be converted to ANSI before it can be read. Similarly, to read newer (Unicode-encoded) ZMX files in older (ANSI-encoded) versions of Zemax OpticStudio, a similar approach must be used. 

There are three options to convert text files from Unicode to ANSI so that they can be read in MATLAB.

1. Text files may be copied directly from Zemax OpticStudio to the Windows clipboard as ANSI by changing the default encoding in Zemax OpticStudio to ANSI. Alternatively, you can copy Unicode text to a text editor like Notepad and re-save as ANSI.

2. The GETTEXTFILE keyword can be issued in a ZPL macro to get the Unicode file, which can then be converted within the Marco.

3. An extension can push a Unicode text file into MATLAB and the file can be converted inside of MATLAB.
 

Approach 1: Change Zemax Preferences to ANSI

The default text file encoding Zemax OpticStudio preference is set to save text data as Unicode. By changing the Zemax OpticStudio preference settings, text data can be copied to the clip board as ANSI. The default preference settings can be found in The Setup Tab > System Group > Project Preferences, under section General. Change the “TXT File Encoding” dropdown option to “ANSI”.

project preferences

Alternatively, you can copy Unicode text to a text editor like Notepad and re-save with ANSI encoding.

SampleTextfile

Approach 2: Convert to ANSI in a ZPL Macro

The key word GETTEXTFILE can be used in a ZPL macro to create a text file from any Zemax OpticStudio analysis window that supports text. The GETTEXTFILE defaults to Unicode- encoding regardless of the Zemax Preference settings. The default Unicode-text can be converted into ANSI in the Macro by using the CONVERTFILEFORMAT keyword.

From Zemax OpticStudio Help Files:

convertfileformat

Approach 3: Convert to ANSI in MATLAB

Once a Unicode text file is loaded into MATLAB it can be converted to ANSI using the “unicode2native” command. The following link contains a description and examples of this keyword: http://www.mathworks.com/help/techdoc/ref/unicode2native.html.
 

 

Summary

To read a text file from Zemax OpticStudio into MATLAB or other ANSI-encoded application, the default Unicode-encoding must be converted to ANSI by changing the preference settings, converting in a ZPL macro, or converting the text file to ANSI in the application, such as using the unicode2native command in MATLAB.