MATLAB: Adding datestr to filename producing odd results

datestr filename xls excel xlsx

Hello,
I am trying to have my data saved as an xls file with the date included.
formatSpec = 'Run_%i_%i.xlsx';
A1 = Run; %in this case I'd declared it to be 37
A2 = datestr(now, 'ddmmmyy');
str = sprintf(formatSpec,A1,A2)
While I can save as "Run_37.xls" and it works. I can't get the date in there.
This is what appears in the cmd window for the variable str, which is what I use in xlswrite(..)
str =
Run_37_48.xlsxRun_51_77.xlsxRun_97_121.xlsxRun_49_54.xlsx
I have no idea what all the extra characters are after Run_37… My variable A2 does produce 03May16.
Thanks for your attention!

Best Answer

Run=37
A1 = Run; %in this case I'd declared it to be 37
A2 = datestr(now, 'ddmmmyy')
str=sprintf('Run_%d_%s.xlsx',A1,A2)