How do I write variables to a text file with date string in format mm/dd/yyyy and three columns of numerical data and add headers?
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Azzi Abdelmalek
2015-6-19
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!