How to avoid e+ notation when using fprintf to print to a file

20 ビュー (過去 30 日間)
Jeffrey
Jeffrey 2011 年 6 月 20 日
Hi,
I am having trouble having matlab avoid using the +e notation when trying to print a number to a txt file. Here is my code:
fid_hex = fopen('result_hex.txt','r');
fid_dec = fopen('result_dec.txt','w');
while ~feof(fid_hex)
hex_val = fgets(fid_hex);
dec_val = hex2dec(hex_val);
fprintf(fid_dec ,'%d\n',uint32(dec_val));
disp(uint32(dec_val));
end;
The disp works fine and Matlab outputs the decimal value in the exact format that I want but the fprintf simply doesn't. Any ideas how to format the fprintf to avoid the e+?
uint32(hex2dec('F4CF2F5F')) = 4107218783
I want Matlab to write 4107218783 to the file; not 4.107219e+009
Thanks!
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 6 月 20 日
Note: as you are using text files, you should use 'rt' and 'wt' instead of 'r' and 'w' for your fopen()'s.

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

回答 (3 件)

Jeffrey
Jeffrey 2011 年 6 月 20 日
anyway... i resorted to using %.0f and it worked.. thanks for all of your help..
fprintf(1,'%.0f\n',uint32(4107218783))

Fangjun Jiang
Fangjun Jiang 2011 年 6 月 20 日
fprintf(1,'%ld\n',4107218783)
doc sprintf
  1 件のコメント
Jeffrey
Jeffrey 2011 年 6 月 20 日
I ran this and it didn't work..

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


Walter Roberson
Walter Roberson 2011 年 6 月 20 日
fprintf(fid_dec ,'%ld\n',uint32(dec_val));
Notice the 'l' (lower-case L) before the 'd': it needs to be used for values greater than 2^31-1
  2 件のコメント
Jeffrey
Jeffrey 2011 年 6 月 20 日
the l before the d doesn't seem to work...
Walter Roberson
Walter Roberson 2011 年 6 月 20 日
Tested fine for me:
>> sprintf('%ld\n',uint32(hex2dec('F4CF2F5F')))
ans =
4107218783
Fangjun Jiang's answer worked fine for me as well.
Which MATLAB version are you using? Are you using an early version 5 release perhaps?

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by