Printing to specific lines in a text file

1 回表示 (過去 30 日間)
Danny Coles
Danny Coles 2013 年 11 月 11 日
コメント済み: Simon 2013 年 11 月 12 日
I have written code to generate 129 different 9 x 4 cells that contain text and numbers. I would now like to print all of these cells to a single text file, with each cell printed below the last one. How can I do this?

回答 (1 件)

Simon
Simon 2013 年 11 月 11 日
Hi!
It is not difficult if the same columns are text and numbers. You can simply loop over all data and printf with "fprintf".
Do you have 129 separate variables? If so, think about storing them in a structure (or 3d cell array) for easier access. If you have a 3d cell array, use
for n = 1:129
for r = 1:9
fprintf(fid, '%d %s %s %f\n', M{n, r, 1}, M{n, r, 2}, M{n, r, 3}, M{n, r, 4});
end
end
of course you need file handling with fopen/fclose in addition.
  3 件のコメント
Walter Roberson
Walter Roberson 2013 年 11 月 11 日
fid = fopen('TheFileName.txt');
and at the end
fclose(fid);
Simon
Simon 2013 年 11 月 12 日
Hi!
In addition to Walter's answer you should use
fid = fopen('TheFileName.txt', 'w');
to be able to write to the file. The fopen-command without permission is for read access only.

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

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by