append .txt file

5 次查看(过去 30 天)
ricco
ricco 2011-11-22
I have a vector of DateTime in the format yyyy-mm-dd HH:MM:SS and a matrix of data, DateTime and data have the same number of rows so I would like to combine them into one matrix but am not sure if this can be done with the format that DateTime is in? After combining them into one matrix, the data will be imported into a .txt file with the corresponding headers. My code is:
DateTime=datestr(datenum('2009-02-02 13:00:00','yyyy-mm-dd HH:MM:SS')...
:4/(60*24):datenum('2009-02-03 13:00:00','yyyy-mm-dd HH:MM:SS'),...
'yyyy-mm-dd HH:MM:SS');
DateTime=cellstr(DateTime);
data=rand(361,12);
data=[DateTime,data];%this part doesnt work (dimensions not consistent).
%import into .txt file
outfile = '/path/to/file/output.out';
header = {'DateTime','data1','data2','data3','data4','data5','data6','data7',...
'data8','data9','data10','data11','data12'};
fid = fopen(outfile, 'w');
if fid == -1; error('Cannot open file: %s', outfile); end
fprintf(fid, '%s\t', header{:});
fprintf(fid, '\n');
fclose(fid);
dlmwrite(outfile,data,'delimiter','\t','-append');
However,if it is not possible to combine DateTime and data into one matrix I would like to know if it is possible to import DateTime first and then append the .txt file with data i.e. I would have to firstly import the header, then import DateTime (starting at the second row in the frist column), then import data (starting at the second row in the second column).
many thanks

采纳的回答

David Young
David Young 2011-11-22
To combine DateTime and data, they both need to be cell arrays, because one of them contains strings. This will work to produce a combined array
combined = [DateTime, num2cell(data)];
However, that won't solve your problem, because you can't write a cell array with dlmwrite.
One possibility is to do something like the code below, using fprintf to do the tab-separated data. Note that this code uses the original numerical data matrix, and that you may need to fiddle with the format for the numbers to get the precision you need.
outfile = 'path/output.out';
header = { ...
' DateTime', ...
'data1','data2','data3','data4', ...
'data5','data6','data7','data8', ...
'data9','data10','data11','data12'};
fid = fopen(outfile, 'w');
if fid == -1; error('Cannot open file: %s', outfile); end
fprintf(fid, '%10s\t', header{:});
fprintf(fid, '\n');
for ii = 1:size(data, 1)
fprintf(fid, '%s\t', DateTime{ii});
fprintf(fid, '%10.6g\t', data(ii,:));
fprintf(fid, '\n');
end
fclose(fid);

更多回答(1 个)

Walter Roberson
Walter Roberson 2011-11-22
data = [DateTime, mat2cell(data,ones(size(data,1),1),size(data,2))];
However, dlmwrite() cannot combine string and numeric data.
After your line
fprintf(fid, '\n');
put in
dataout = data.';
fprintf(fid, '%s\t%d\n', dataout{:});
and remove the dlmwrite()
  2 个评论
David Young
David Young 2011-11-22
Walter, doesn't that print just two data values per line?
ricco
ricco 2011-11-22
This doesn't work as I was expecting. Firstly it converts DateTime into decimals and secondly it only produces 2 vectors the first vector of time and only one vector of data, leaving 10 empty columns.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by