How to Load the Variables in a .MAT File into the Workspace in MATLAB

The function load loads variables from a file into the workspace.


load(filepath) loads all variables in the .MAT file filepath into the workspace. If filepath only indicates the filename, such as ‘myfile.mat’, MATLAB searches the file in the current directory and in the search path.

MATLAB generates an error if filepath does no exist.
% Load all variables in the file 'workspace.mat' into the workspace

Note: Variables in filepath overwrite current variables in the workspace that have the same name.


Apply this procedure to make the workspace variables persistent between sessions, so you can use in the current session variables created in previous sessions:

  1. Add Code 1 to the ‘startup.m’ file
  2. Add Code 2 to the ‘finish.m’ file
% Code 1
userPathArray = strsplit(userpath,';');
filepath = fullfile(userPathArray{1},'workspace.mat');
if(exist(filepath,'file')) load(filepath); end        
% Code 2
userPathArray = strsplit(userpath,';');

Further reading

I recommend the following books to learn more on the MATLAB environment.

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    This book was the winner of a Textbook Excellence Award. The first chapter explains the windows of the MATLAB environment, among other introductory topics like operators, random numbers, and trigonometry.

  2. MATLAB for Engineers (5th Edition)

    This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. The second chapter explains the windows of the MATLAB environment, how to name variables, and how to save a series of commands in an .M file, among other topics.

I also recommend the following page in the MATLAB help documentation, and the other tutorials in this series.

doc load;

Source code

I hope you have learned how to load the variables in a .MAT file into the workspace in MATLAB. The source code developed in this tutorial is available at this page.


Your friends may need this knowledge to complete a task. Share this tutorial.