How to ensure MATLAB will read your text file

This article explains what to do if MATLAB will not read your text file.

Authored By Liz Gerrish and Alissa Wersal



In 2010, the default text encoding in OpticStudio was changed from ANSI to Unicode - an industry standard for representation of worldwide text sets. While most programs can easily process Unicode-encoded files, MATLAB is one that requires conversion to ANSI in order to read external text files.

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

1. Change the OpticStudio default or use Notepad to convert.

2. Use the GETTEXTFILE keyword in a ZPL macro.

3. Use an extension to push a Unicode file into MATLAB for it to be converted there.

This article will provide an overview of each method.

Approach 1: Change OpticStudio preferences to ANSI

The default text file encoding OpticStudio preference is set to save text data as Unicode. By changing the 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”.




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




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 OpticStudio analysis window that supports text. The GETTEXTFILE defaults to Unicode-encoding regardless of the Preference settings. The default Unicode-text can be converted into ANSI in the Macro by using the CONVERTFILEFORMAT keyword.

From the OpticStudio Help Files:




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:


Was this article helpful?
0 out of 0 found this helpful



Article is closed for comments.