MATLAB: How to write variables to a text file with date string in format mm/dd/yyyy and three columns of numerical data and add headers

stringtext filetime series

I have several variables I want to write to a text file, where the resulting .txt file will be: col1 = time (in cell array of size 5844:1 with format mm/dd/yyyy); col2 = var1; col3 = var2; col4 = var3.
I am having trouble with the date column and cannot seem to correctly write it to the .txt file. I have used fprintf and dlmwrite but I am not understanding how to write string to text. Also, how do I add header? Please help! Angela

Best Answer

date=datestr(datenum('10/01/2014'):datenum('10/10/2014'),'dd/mm/yyyy')
var1=(1:10)'
var2=sin(var1)
header='date var1 var2'
M=[cellstr(date) num2cell(var1) num2cell(var2)]
fid = fopen('fic.txt','w');
myformat='%s %12.4f %12.4f \r\n'
fprintf(fid,'%s\r\n',header);
for k=1:size(M,1)
fprintf(fid,myformat,M{k,:})
end
fclose(fid);