MATLAB: Problem getting error while trying to save .mat file in every folder

.mat filedirectoryfolderfor loopsavesub directories

I have 44 folders and I have a matlab script which runs for loop for every folder and save .mat file. But when I save .mat file inside the for loop, the matlab file doesn't save inside every folder but save in main folder. I want to save the mat file in every folder that I am running my script but it is not working. Is there any method to save .mat file in every folder of my for loop?

Best Answer

You need to tell MATLAB the absolute or relative path of the directory where you want to save those files, something like this (you can fill in the missing data yourself):
for k = 1:N
...
k_path = ...
k_name = ...
k_full = fullfile(k_path,k_name);
save(k_full,...)
end
In any case, how to process a sequence of files is covered in the documentation and this forum (and folders uses exactly the same methods):
etc etc