Saving a .txt with Month/Day in filename

조회 수: 1 (최근 30일)
Michael
Michael 2013년 10월 21일
댓글: Image Analyst 2013년 10월 22일
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
Matt Kindig
Matt Kindig 2013년 10월 21일
편집: Matt Kindig 2013년 10월 21일
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');
Michael
Michael 2013년 10월 22일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2013년 10월 21일
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
Michael
Michael 2013년 10월 22일
Thank you. Which method would prefer and why?
Image Analyst
Image Analyst 2013년 10월 22일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by