Search code examples
matlabmat-file

How to load specified variables from a mat file


I have some mat files with large number of variables saved in it. So I only want to load a subset of the variables. For example, if I want to load

vars = {'x', 'y', 'z'}

I know I can explicitly do

a = load('filename.mat', 'x', 'y', 'z')

but the vars list can get long and I need to load multiple files, so I can't explicitly list out vars list every time. Is there a way to pass in vars as a argument input?


Solution

  • You can pass your cell array of variable names as a comma-separated list like so:

    a = load('filename.mat', vars{:});