Effacer les filtres
Effacer les filtres

How To Write a Long-Decimal Number Data With Fprintf?

8 vues (au cours des 30 derniers jours)
Tyann Hardyn
Tyann Hardyn le 22 Mai 2022
Commenté : Walter Roberson le 22 Mai 2022
Hi, Community...
I have a problem in writing a data that re contain with Long-Decimal numbers.... So i have this data :
annual_equation
ans =
1.0e+04 *
0.0000
-0.0000
4.1354
And if i change it in string data :
annual_equation =
3×1 string array
"3.39982917e-15"
"-1.67130873e-08"
"41353.5216"
I just want to print it in txt file, but the only number which re writed is just the last one :
By the way, im writing the file above with this code :
kode_annual = string(transpose(annual_equation));
for kode = 1:length(kode_annual)
dir_kode = string(filefive_path);
dir_kode2 = convertStringsToChars(filefive_path);
multi_k_annual = dir_kode+sprintf('Kode Annual Mean (%s).txt', judul_kode);
isian_kode{kode} = kode_annual(kode);
eval(['cd ''' dir_kode2 ''';']);
fout_Kode = fopen(multi_k_annual,'w');
fprintf(fout_Kode, '%s\n', [isian_kode{kode}]');
fclose(fout_Kode);
end
It maybe simple for you all to solve this problem, but its really kinda difficult for me.... Is there anything wrong with my code? Im so grateful if someone can help my problem here... Thank you so much.... /.\ /.\ /.\

Réponse acceptée

Walter Roberson
Walter Roberson le 22 Mai 2022
kode_annual = string(transpose(annual_equation));
change that to
kode_annual = compose("%.10g", annual_equation);
It is important for this purpose that you use double-quotes rather than single quotes for the format.
  3 commentaires
Tyann Hardyn
Tyann Hardyn le 22 Mai 2022
cd(dir_kode2);
Alright SIr, noted that..
But the output in my txt file is still same Sir :
Its only showed the last coefficient of the equation : 41355.94047
althought i ve been changed the code to become :
kode_annual = compose("%.10g", annual_equation);
Walter Roberson
Walter Roberson le 22 Mai 2022
In each iteration of the for kode loop, you open the same file for 'w' access, and write one string to the file, and fclose() it. However, 'w' access explicitly requests that any current content of the file is to be deleted. So your code only leaves whatever was last written into the file.
You have two possibilities:
  • if you are sure that the filename is not going to change, then fopen() before the loop, and fclose() after the loop
  • if the filename might change, then fopen() with 'a' access inside the loop.
Note:
multi_k_annual = dir_kode+sprintf('Kode Annual Mean (%s).txt', judul_kode);
Be careful with that. That code depends on dir_kode have a path separator that works for the current operating system. It would be more robust to use
multi_k_annual = fullfile(dir_kode, sprintf('Kode Annual Mean (%s).txt', judul_kode));

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by