MATLAB: Unable to generate by code name of file for save command

save string char filename

I am trying to save a file (preferably .mat) every time a loop is finished. The following throws an error: for j = 1:2; do lots of stuff; filename = ['X',num2str(j),'.mat']; save(filename,variable); end
Apparently filename is char rather than string; but when I try instead: for j = 1:2; do lots of stuff; filename = ['X',num2str(j),'.mat']; save(string(filename),variable); end
Matlab says string is obsolete! No way out so far…. any help for this seemingly innocuous issue greatly appreciated.

Best Answer

Hi Tom,
Try
filename = ['X',num2str(j),'.mat']; save(filename,'variable'); end
i.e the name of the variable is in single quotes.