How do I import data into MATLAB?

Y
ytnhg9 · Jul 29, 2025 · 333 views
Question
How do I import data into MATLAB?
Expert Answer
Profile picture of Neeta Dsouza
Neeta Dsouza PhD Expert
Answered Sep 20, 2026

In MATLAB, the modern and recommended way to import data is using readtable() for mixed-type datasets (text, dates, numbers) and readmatrix() for pure numeric arrays from CSV, Excel, or text files. You can also import data interactively using the GUI Import Tool via uiimport(), load native binary variables using load(), fetch live JSON payloads with webread(), or stream out-of-memory big data using datastore().

Top 5 Modern Methods to Import Data in MATLAB

1. Importing CSV and Delimited Text Files

Use readtable() to retain column headers and mixed data types, or readmatrix() when you only need raw numbers:

% Import as a structured MATLAB Table (Preserves headers & variable types)
dataTable = readtable('sensor_measurements.csv');
head(dataTable); % View first 8 rows

% Access specific table columns by name
temperatures = dataTable.Temperature;
timeStamps = dataTable.Time;

% Import as pure double matrix (Numeric values only)
numericMatrix = readmatrix('sensor_measurements.csv');

2. Importing Excel Spreadsheets (.xlsx, .xls)

Read specific sheets, cell ranges, or entire workbooks directly into MATLAB without needing external COM servers:

% Import a specific sheet and cell range
financialData = readtable('Quarterly_Report.xlsx', ...
    'Sheet', 'Q3_Results', ...
    'Range', 'B2:F150', ...
    'VariableNamingRule', 'preserve');

% Preview data table summary
summary(financialData);

3. Loading Native MATLAB Binary Files (.mat)

Load workspace variables saved from prior MATLAB sessions:

% Load all variables from a .mat file into workspace
load('trained_neural_network.mat');

% Load specific variables only into a struct to prevent overwriting workspace
dataStruct = load('experiment_results.mat', 'voltage', 'current');
v = dataStruct.voltage;
i = dataStruct.current;

4. Fetching REST APIs & JSON Web Endpoints

Read live cloud data, IoT sensor feeds, and REST APIs directly:

% Fetch JSON data from an API endpoint
apiURL = 'https://api.weatherapi.com/v1/current.json?key=YOUR_KEY&q=London';
weatherData = webread(apiURL);

% Convert struct payload into a table
temp_c = weatherData.current.temp_c;
humidity = weatherData.current.humidity;
fprintf('Current Temperature: %.1f C, Humidity: %d%%\n', temp_c, humidity);

5. Big Data & Massive Files (Out-of-Memory)

For large datasets that exceed available system RAM (e.g., 50 GB CSV or Parquet files), use a datastore paired with tall arrays:

% Create a datastore reference (does not load full file into RAM)
ds = tabularTextDatastore('large_flight_dataset.csv', 'TreatAsMissing', 'NA');

% Create tall array for out-of-memory computation
tt = tall(ds);

% Compute summary metrics lazily across chunks
meanAltitude = mean(tt.ActualAltitude, 'omitnan');
result = gather(meanAltitude); % Evaluates and returns final scalar

Interactive GUI Import (No Code Required)

If you prefer a visual interface, type uiimport('filename.csv') in the Command Window or navigate to the Home Tab → Import Data. The Import Tool allows you to interactively highlight cell regions, select delimiter rules, set missing value handlers (e.g., fill with NaN or mean), and automatically generate a reusable MATLAB function.

Modern vs. Deprecated Import Functions

File Type Modern Recommended Function Deprecated Function (Avoid)
Mixed CSV / Text Table readtable('data.csv') textscan(), importdata()
Numeric CSV Matrix readmatrix('data.csv') csvread(), dlmread()
Excel Spreadsheets readtable('data.xlsx') xlsread()
Cell Array of Strings readcell('data.csv') textread()
Time-Series Timetables readtimetable('data.csv') Manual table-to-timetable conversion

Key Best Practices

  • Avoid Deprecated Functions: Replace legacy functions like xlsread() and csvread() with readtable() or readmatrix(). The modern functions are multi-threaded, faster, and cross-platform.
  • Preserve Column Names: Use 'VariableNamingRule', 'preserve' in readtable() if your column headers contain spaces or special characters.
  • Manage Missing Data: Configure 'TreatAsMissing', {'NA', 'null', '-999'} to automatically convert missing placeholders into standard MATLAB NaN values.
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!