3
votes

The following code generates a similar dataset to what I am currently working with:

clear all
    a = rand(131400,12);
    DateTime=datestr(datenum('2011-01-01 00:01','yyyy-mm-dd HH:MM'):4/(60*24):...
        datenum('2011-12-31 23:57','yyyy-mm-dd HH:MM'),...
        'yyyy-mm-dd HH:MM');
    DateTime=cellstr(DateTime);
    header={'DateTime','temp1','temp2','temp4','temp7','temp10',...
        'temp13','temp16','temp19','temp22','temp25','temp30','temp35'};

I'm trying to convert the outputs into one variable (called 'Data'), i.e. have header as the first row (1,:), 'DateTime' starting from row 2 (2:end,1) and running through each row, and finally having 'a' as the data (2:end,2:end) if that makes sense. So, 'DateTime' and 'header' are used as the heading for the rows and column respectively. Following this I need to save this into a tab delimited text file.

I hope I've been clear in expressing what I'm attempting.

1
This might make it more clear: give the first few rows of expected output for your Data variable. - St-Ste-Ste-Stephen

1 Answers

3
votes

An easy way, but might be not the fastest:

Data = [header; DateTime, num2cell(a)];
filename = 'test.txt';
dlmwrite(filename,1); %# no create text file, not Excel
xlswrite(filename,Data);

UPDATE: It appears that xlswrite actually changes the format of DateTime values even if it writes to a text file. If the format is important here is the better and actually faster way:

filename = 'test.txt';
out = [DateTime, num2cell(a)];
out = out'; %# our cell array will be printed by columns, so we have to transpose

fid = fopen(filename,'wt');
%# printing header
fprintf(fid,'%s\t',header{1:end-1});
fprintf(fid,'%s\n',header{end});
%# printing the data
fprintf(fid,['%s\t', repmat('%f\t',1,size(a,2)-1) '%f\n'], out{:});
fclose(fid);