Search code examples
matlabmatlab-load

Load Multiple .mat Files to Matlab workspace


I'm trying to load several .mat files to the workspace. However, they seem to overwrite each other. Instead, I want them to append. I am aware that I can do something like:

S=load(file1)
R=load(file2)

etc.

and then append the variables manually.

But there's a ton of variables, and making an append statement for each one is extremely undesirable (though possible as a last resort). Is there some way for me to load .mat files to the workspace (by using the load() command without assignment) and have them append?


Solution

  • Its not entirely clear what you mean by "append" but here's a way to get the data loaded into a format that should be easy to deal with:

    file_list = {'file1';'file2';...};
    for file = file_list'
        loaded.(char(file)) = load(file);
    end
    

    This makes use of dynamic field references to load the contents of each file in the list into its own field of the loaded structure. You can iterate over the fields and manipulate the data however you'd like from here.