Changing the Serial Date to String when saving as .txt file

조회 수: 1 (최근 30일)
Pw
Pw 2014년 3월 6일
댓글: Pw 2014년 3월 7일
I have a 20197x44 matrix where the first column is the serial date. I want to change the serial date to the format 'mmm dd, yyyy HH:MM:SS.FFF AM' when I am saving to a text file. (This data is for a client who has requested the date in this format).
I cannot figure out how to do this successfully. I have tried several approaches that are close but not working. I used a code to add column headers (shown below). Would I do something similar but use columns instead of rows?
% SAMPLE 1:
filename = 'Sample 1 Data.txt';
fileID = fopen(filename,'w+');
[rows, columns] = size(h1);
for index = 1:rows
fprintf(fileID, '%s,', h1{index,1:end-1});
fprintf(fileID, '%s\n', h1{index,end});
end
fclose(fileID);
dlmwrite(filename, rod1,'-append', 'delimiter', ',');
where h1 is the cell array of the column titles. Any suggestions greatly appreciated. Thank you
- Pamela

채택된 답변

Walter Roberson
Walter Roberson 2014년 3월 6일
fprintf(fileID, '%s\n', datestr(h1(:,1), 'mmm dd, yyyy HH:MM:SS.FFF AM'))
The place it gets more complicated is if you want to put additional information on the same row and you want to do it all with one fprintf() instead of looping over the rows.
  댓글 수: 1
Pw
Pw 2014년 3월 7일
Aw! Thank you.
The rest of the row contains numeric data. I don't need to do it all in one fprintf(), looping would be fine. How would I write this loop? I'm not as familiar with loops as I'd like to be so learning how to write one for this would be a great exercise. I'm assuming I could use a similar format as the one I used for the column headers....?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by