필터 지우기
필터 지우기

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