Search code examples
matlabworkspace

MATLAB - store variables with a specific prefix in .mat file


Is there a way to store just variables with a specific prefix inside a .matfile?

Example workspace variables:

a
b
c
d1
d1123fdwfwe
d23we
e
f

I am looking for a way to store for example just the variables with the prefix 'd' or the prefix 'd1' in a matfile.


Solution

  • You can use regular expressions in save to specify which variables are saved.

    To save variables that begin with d1:

    save('filename','-regexp', 'd1.*')
    

    The regular expression 'd1.*' indicates 'd1' followed by zero or more characters.