File Exchange

image thumbnail


version 2.0 (21.4 KB) by Rik
read any (text) file, either UTF8 or ANSI


Updated 28 Jun 2020

GitHub view license on GitHub

If you want to read a (text) file with Matlab, you need to know what encoding it is: UTF-8 (the 'modern' standard) or ANSI (US-ASCII, the 'old' standard). If you have files you want to read automatically, where you don't necessarily know the encoding you would have to guess. This will sometimes result in strange text that you only notice 3 or 4 steps later.

This submission takes care of that problem by providing a single way of reading a file. It preserves leading and trailing spaces, it preserves empty lines, and it can handle both UTF-8 files and ANSI files. Note that although the encoding should be specified in a special leading bit, it is not possible to read this with Matlab. It is therefore possible that a file is read with the wrong encoding, although this should be a very rare occurrence.

Licence: CC by-nc-sa 4.0

Cite As

Rik (2020). readfile (, GitHub. Retrieved .

Comments and Ratings (2)

omid zandi

thank you very much!! it works great!

zhiwen wan

It works perfectly!!



See release notes for this release on GitHub:


Adapted subfunction and updated MacOS Siera to Catalina. The function now also warns on Octave for chars >255.


cosmetics and expanded test

MATLAB Release Compatibility
Created with R2020a
Compatible with R13SP1 to any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags