MATLAB: Code to load the files from folder, add workspace variable and save to another folder

adding a variable to mat fileinputMATLABoutput

Dears,
I have a lot of .mat files wich I want to add a variable fs=128 to all of the one by one.
one way is to load ech other, add variable and save, which takes some days because of thousands data.
so I need a code (i.e. for loop) to load each one, add same variable and save it.
please help me.
thank you.

Best Answer

You don't need to load the file data, you can use save's '-append' option:
fs = 128;
for ... loop over all files, you need to finish this line yourself!
fnm = 'name_of_the_file.mat'; % change this depending on how you generate/index the filenames.
save(fnm,'fs','-append')
end