フィルターのクリア

How to write a matrix into a txt file so that all variables in a columns have exactly the same starting character and length?

1 回表示 (過去 30 日間)
I am trying to export a matrix into a text file. The text file will later be feed into another program that requires all variables in a specific columns have the same starting location, and the same length.
How should I achieve this? Which Matlab function should I use to export the data?
Thank you.

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 4 月 21 日
use sprintf formating, such as
a=rand(5,1);
sprintf('%20.2f\n',a(:));
doc sprintf
It also applies to fprintf.
  2 件のコメント
Liqing
Liqing 2011 年 4 月 21 日
Thank you very much for the suggestion. So for sprintf I will have to write one row at one time?
Walter Roberson
Walter Roberson 2011 年 4 月 21 日
No, sprintf() and fprintf() will repeat the format as often as needed to write out all the data.

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

その他の回答 (2 件)

Liqing
Liqing 2011 年 4 月 21 日
Now it is working. Thank you very much.
Another question. Now the deliminator is space, so that length of each row is very different. For example, in a column, '45' will have different length as '-45", '26.7', will have different length as '126.8'.
How could I fix this?

Fangjun Jiang
Fangjun Jiang 2011 年 4 月 25 日
What do you mean? What exact format do you want to achieve? Please read through the document of sprintf formatting. I think it should be able to cover most if not all format.
Is this what you need?
sprintf('%010.2f\n',45,-45,26.7,126.8)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by