Search code examples
matlabexportunix-timestamp

MATLAB exporting datestr into txt file


So I have this code,

%%
%Changing from the unix epoch time to standard DDMMYY format
%Importing the data 
unix_time = [1435763099;1435763109;1435763119;1435763129]; %time of the plot

%Conversion of the epoch data
time = datestr(719529+unix_time/86400,'dd/mmm/yyyy HH:MM:SS');

And I would like to export the 'time' into a .txt file or excel however I can't find a way to do so without resulting in wrong txt file.

fid = fopen('time.txt','wt');
fprintf(fid, time)
fclose(fid);

I tried this method but it doesn't seem to give me the correct result i'm looking for which is that I want the results to be the unix_time converted to the dd/mmm/yyyy HH:MM:SS format and export it into a column text file.

I wan't the result to display this in a text file

01/Jul/2015 15:04:59,
01/Jul/2015 15:05:09,
01/Jul/2015 15:05:19,
01/Jul/2015 15:05:29,

Solution

  • I think in this case its best to use a different function than fprintf.

    dlmwrite will do the job just fine if used as following:

     dlmwrite('time.txt',time,'delimiter','');
    

    However, this won't add the comas in the end of each row. If you want the comas, you can just add them to the variable time using matrix indexing, before you write it in the file.

    time(:,end+1):',';