Effacer les filtres
Effacer les filtres

dlmwrite -append problems when writing char and numbers to .txt file

1 vue (au cours des 30 derniers jours)
nori
nori le 22 Juil 2011
I am having troubles trying to write a mix of char and numbers to a delimited file.
that dlmwrite cannot handle both char and num and must use a cell array and thus dlmwrite treats each char as a separate element and places a delimiter between each char and number.
basically what i am doing is reading in a large csv files with mix of char and numbers and sorting through the file line by line and only copying the lines that are for the same location (based on a unique id code) to a new file. the data is a mixed bag of temp for all the met stations in tanzania.
example line from my data: "TANZANIA,UNITED REPUBLIC OF",TZ03MSKR,,MUSEKERA,-9.3,33.6,12,1/1/1943,139
once found, i want to copy the exact line to a new file. most importantly i need to append each line to the existing file as my original file is for 35+ years of monthly readings for each 129 different stations.
i got everything working except the append part.
essentially its the same problem as this
filename = 'C:\test.txt';
str = 'test123';
dlmwrite(filename, str, '-append')
returns
t,e,s,t,1,2,3
i was wondering if anyone can help point out a workaround that is different from the quoted thread or is this the best method?
Thanx.

Réponse acceptée

Oleg Komarov
Oleg Komarov le 22 Juil 2011
If you have an example input as, use fprintf:
out = {'"TANZANIA,UNITED REPUBLIC OF"','TZ03MSKR','MUSEKERA',-9.3,33.6,12,'1/1/1943',139
'"My Republic"','TZ03MSKR','MUSEKERA',3,3.6,1,'1/21/1941',111};
out = out.';
fid = fopen('test.csv','a');
fprintf(fid,'%s,%s,%s,%f,%f,%f,%s,%f\r\n',out{:});
fid = fclose(fid);
  1 commentaire
nori
nori le 23 Juil 2011
thanx Oleg!
the fprintf worked perfectly.
thanx a bunch.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Analysis 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!

Translated by