append .txt file
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
David Young
le 22 Nov 2011
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);
0 commentaires
Plus de réponses (1)
Walter Roberson
le 22 Nov 2011
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 commentaires
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!