フィルターのクリア

Write data to csv, headings start with numbers

4 ビュー (過去 30 日間)
Pippa Williams
Pippa Williams 2018 年 7 月 26 日
コメント済み: Walter Roberson 2018 年 7 月 27 日
I want to write some data to .csv file, but the headers I need (to keep the format the same as previous files) start with numbers. So I guess I have to write the first line as a char vector, and then write the data.
I tried:
writeTable(OutputData,OutputFileName);
NewHeaderLine = 'DateTime,40cumecs,50cumecs,60cumecs,70cumecs,80cumecs';
fid = fopen(OutputFileName,'w');
fprintf(fid,'%s\n',NewHeaderLine);
fclose(fid);
But this clears the additional data. How can I write both data and non-standard text to a .csv file? I've looked at a bunch of help files but all seem to append numeric data rather than text, so they don't apply in this case.

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 7 月 26 日
Perhaps write the header first like you do, and then use dlmwrite() with -append
  2 件のコメント
Pippa Williams
Pippa Williams 2018 年 7 月 27 日
Thanks, that nearly gets me there but my data includes DateTime values, and I don't think dlmwrite() can handle strings? Is there some other way I can write data that includes strings?
I thought maybe it would work better if I convert all my data to a string array before I write it?
Walter Roberson
Walter Roberson 2018 年 7 月 27 日
Unfortunately writetable() cannot append to text files.
Sometimes the easiest way is to write the two parts (header and table) to different files and then use operating system utilities to splice them together. In particular for MS Windows,
system('copy FileName1+FileName2 FileName3')
effectively appends FileName1 and FileName2 to form FileName3

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by