Saving a .txt with Month/Day in filename

Hello MW forum!
I'd like to save a big matrix to an ascii file as double, but I'd like the date (preferably the month/day) added to the filename before the '.txt'...
I was using something like:
save('good_series.txt', matrix_variable, '-ascii', 'double')
and Ive tried a few other things where I try to pass the date in as an argument to a filename, but it was unsuccessful. I'd like to avoid using solutions with f* anything.. fid, fopen, etc.
Thank you in advance for your suggestions and help.
Sincerely,
Michael

4 Comments

Why not something like this:
filename = sprintf('good_series%s.txt', datestr(now,'dd-mmm-yyyy'));
save(filename, matrix_variable, '-ascii', 'double');
Hey Matt,
This looks great but I'm getting an error:
Error using save Argument must contain a string.
Yeah this is similar to errors I got before, but I don't where I'm going wrong... the string filename is defined and correct!... the matrix itself is defined... what could it be?
I'm sorry I couldn't get it right!
Thanks again, Michael
Is 'matrix_variable' the actual name of the variable to save, or is it a string containing the name of the variable? Because if it is the former, you need to wrap it in quotes, like:
filename = sprintf('good_series%s.txt', datestr(now,'dd-mmm-yyyy'));
save(filename, 'matrix_variable', '-ascii', 'double');
Hey this works THANK YOU, PS this is the second time you've correctly answered my query and I couldn't upvote it or accept it! What gives?

Sign in to comment.

 Accepted Answer

Use sprintf() to construct the filename from the date string:
formatOut = 'dd-mmm-yyyy';
d=datestr(now, formatOut)
baseFileName = sprintf('%s, good_series.txt', d)
fullFileName = fullfile(yourFolder, baseFileName);
save(fullFileName, matrix_variable, '-ascii', 'double')

6 Comments

How do I format the variable 'yourFolder' ? It might be really convenient to have no absolute references to any folder.. would this be possible?
yourFolder = uigetdir(); % User picks folder.
or somehow specify it otherwise, like hard code it in
yourFolder = pwd; % Use "Current Folder"
yourFolder = 'D:\myImages';
That doesn't put the date in the file name.. even when using the pwd solution.. I think the problem is:
sprintf('%s, good_series.txt', d)
Does that sound reasonable? What should I do? I feel so close...
Not sure why you say that. I ran this:
formatOut = 'dd-mmm-yyyy';
d=datestr(now, formatOut)
baseFileName = sprintf('%s, good_series.txt', d)
yourFolder = 'C:\';
fullFileName = fullfile(yourFolder, baseFileName)
and got this:
d =
21-Oct-2013
baseFileName =
21-Oct-2013, good_series.txt
fullFileName =
C:\21-Oct-2013, good_series.txt
Don't you see 21-Oct-2013 in the filename??? That is today's date. And it's in fullFileName, which is the filename your data will be saved into. I don't understand why you say the date is not in the filename when I can see clearly that it is .
Thank you. Which method would prefer and why?
It creates a filename with a date encoded into it. Of course, you then use that filename in the save() function to save whatever variables you want (such as the variable matrix_variable that you used) into that file. My code had
save(fullFileName, matrix_variable, '-ascii', 'double')
in it to save the variable matrix_variable into a 16 bit ASCII text file (which you want for some reason according to your original code). (This was in my initial Answer, not the comment which was to just explain that the filename did indeed have the date in it).

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!