how to split the numbers into three different colums in fprint command ?

3 ビュー (過去 30 日間)
Adeline War
Adeline War 2023 年 5 月 15 日
移動済み: Walter Roberson 2023 年 5 月 21 日
How to split the numbers into three different colums with 6 places after the decimal point in fprint command such that the next number written will be in a next line as well?
This is how I am getting
GradVec = 1.0000000.6593560.000000 % -- I want to split these at the 6th place after the decimal using fprintf
diary ('F:\new_code\gradation.txt');
fprintf('%6.6f',GradVec);
diary off
  2 件のコメント
Walter Roberson
Walter Roberson 2023 年 5 月 15 日
GradVec = 1.0000000.6593560.000000
It is an error to have more than one decimal point character in a numeric literal.
Do you have text in that format? If so are you trying to read text in that format from a file? It would be quite uncommon to have text defined in that format: it would be far more common for there to be fixed width fields. (Well, unless the text was written by poorly planned C code.)
Adeline War
Adeline War 2023 年 5 月 15 日
@Walter Roberson Assume that value is given as
GradVec = [x(1) x(2) x(3)]
such that x(1) = 1.0
x(2) = 0.6
x(3) = 0.9
but when i fprint it on text
I get it as
1.00.60.9
How do i fix this?

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

回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 5 月 15 日
移動済み: Walter Roberson 2023 年 5 月 21 日
When you use an fprintf() format and pass in more values than there are % items inside the format, the format gets reused from the beginning exactly as is. So if you use a '%.6f' format and pass in three items, you have effectively used repmat('%.6f', 1, 3) which would be like '%.6f%.6f%.6f' . Notice there are not spaces in that format! When you use fprintf() like this, it does not automatically add spaces.
So... use '%.6f %.6f %.6f\n' as your format.
  1 件のコメント
Adeline War
Adeline War 2023 年 5 月 16 日
移動済み: Walter Roberson 2023 年 5 月 21 日
@Walter Roberson Something new I learned today. Thank you so much

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

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by