How can i assign certain length for each column of exported text file?

5 ビュー (過去 30 日間)
Joseph
Joseph 2019 年 7 月 18 日
コメント済み: Joseph 2019 年 7 月 19 日
Hi,
I am trying to write a 8680*18 cell array in to a text file. But i need to assign certain defferent lenght for elements in each column (i.e. all elements in first have 3 characters and all elements in the second column have 12 characters, etc). can anyone help?
Thank you

採用された回答

Walter Roberson
Walter Roberson 2019 年 7 月 19 日
None of the Mathworks supplied tools for writing out matrices support that, so you will need to construct it yourself.
col_widths = [3 12 etc];
fmt_cell = [sprintfc('%%%ds', col_widths), '\n'];
fmt = [fmt_cell{:}];
your_cell_transpose = your_cell.';
fid = fopen('YourFile.txt', 'wt');
fprintf(fid, fmt, your_cell_transpose{:});
fclose(fid);
  3 件のコメント
Walter Roberson
Walter Roberson 2019 年 7 月 19 日
Ah, that is for mixed datatype; your question sort of implied everything was char.
Your format would have to be touched up for fixed width, such as
formatSpec = '%3s %12d %2.1f %s\n';
Note this is for right-justified in field; if you want left-justified, use a negative width, such as %-3s
Joseph
Joseph 2019 年 7 月 19 日
Thanks Walter. You are always helpful.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by