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

1 次查看(过去 30 天)
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.

采纳的回答

Oleg Komarov
Oleg Komarov 2011-7-22
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by