MATLAB save all variables separately

My current workspace contains 32 variable sequentially named. Each variable is an array of doubles with different lengths. I want to save each of them as a separate .mat file such that name of each file is same as the name of the variable. I could not find any help on google or a previously asked question in SE. Help needed urgently.

Note: I need to associate this data with some other text file that I have for each variable separately. So saving in a single file as a struct is not what I am looking for and the name of the variable should be the name of the file. Thanks.

1 Answer

I figured it out myself. This simple code below does the trick. Thanks for trying if anyone did.

vars=who;
for i=1:length(vars)
save(vars{i},vars{i})
end

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like