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?

9 views (last 30 days)
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

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 19 Jun 2015
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);

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!