Cheat Sheets

Importing and Exporting Data Using MATLAB​

MATLAB provides functionality to read and write data in many forms. This reference shows common use cases, but is not a comprehensive list of available functionality.​

Import Tool

Select Import Data to launch the Import Tool.

Low Level I/O

Low-level functions, such as fgetl and fscanf, allow the most control over I/O.​

fid = fopen('myfile.txt');​
data = fscanf(fid,'%f %q');​
fclose(fid);​

Format Specs​

Type Specifier Output Class

Signed int

Unsigned int

%d,%d8,…

%u,%u8,…

int32,int8

uint32,uint8

Floating point

%f

%f32

double

single

Text array

%s, %q

'TextType'

string

Datetime

%D,%{fmt}D

datetime

Duration

%T,%{fmt}T

duration

Category

%C

categorical

Pattern

%[…]

string

Skip field

%*k

 

Standard File Formats

Use datastores for large or multiple files. fileDatastore can be used with any type of file. Use a custom datastore for more advanced control over read behavior.

Specialized I/O support can be found in several add-on products (Simulink, Database Toolbox, Vehicle Network Toolbox , and others. See the File Exchange and GitHub for additional functionality.

Type Single File Multiple Files Write Advanced

Text

readtable

tabularTextDatastore

writetable

detectImportOptions

textscan

Spreadsheet

readtable

spreadsheetDatastore

writetable

detectImportOptions

.mat

load matfile

fileDatastore

save

Custom datastore

Image

imread

imageDatastore

imwrite

Custom datastore

Video

VideoReader

fileDatastore

VideoWriter

Custom datastore

Audio

audioread

fileDatastore

audiowrite

Custom datastore

NetCDF

ncread

fileDatastore

ncwrite

netcdf

CDF

cdfread

fileDatastore

cdfwrite

cdflib

HDF5

h5read

fileDatastore

h5write

H5, H5F, …

XML

xmlread

fileDatastore

xmlwrite

Custom datastore

Binary

fread

fileDatastore

fwrite

Custom datastore

Web Data

RESTful Web Service

webread

Read data

webwrite

Write data

websave

Save data to file

weboptions

Specify options such as authentication and timeout

JSON     

jsondecode jsonencode

HTTP Messaging

Use the HTTP interface for more complex web communication.

body = matlab.net.http.MessageBody(x);
request = matlab.net.http.RequestMessage(method,header,body);