Adding datestr to filename producing odd results

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!

2 Comments

The format string makes no sense: the date is a string, but you use %i for the date variable, whereas it should be %s (because the date variable is a string, not an integer):
'Run_%i_%s.xlsx'
Also note that it would be much better to use an ISO 8601 date format, such as yyyy-mm-dd, because these sort correctly into chronological order.
This makes sense... thank you so much for the quick response, and for a working solution! (I didn't realize that %i %d %s were indicative of integer double and string, respectively...)

Sign in to comment.

 Accepted 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)

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!