How to save Matrix in text-file using format double?

60 ビュー (過去 30 日間)
Holger
Holger 2024 年 11 月 2 日 8:56
コメント済み: Umar 2024 年 11 月 5 日 2:46
I like to save a matrix into a textfile.
save('name','matrix','-ascii') saves matrix in a format
1.93e3 1.32e-2
1.94e3 1.03e-3
but I need:
1930 0.0132
1940 0.00103
How to manage?

回答 (2 件)

Stephen23
Stephen23 2024 年 11 月 2 日 12:05
Using SAVE for text files is very outdated. The recommended approach is to e.g. WRITEMATRIX:
M = [1930,0.0132; 1940,0.00103];
writematrix(M,'mytest.txt')
Checking
type mytest.txt
1930,0.0132 1940,0.00103
  1 件のコメント
Holger
Holger 2024 年 11 月 2 日 12:12
Thanks, that helped me!

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


Umar
Umar 2024 年 11 月 2 日 14:14

Hi @Holger,

You can use the fprintf function instead of save. The fprintf function allows for greater control over the formatting of the output. For more guidance on this function, please refer to

https://www.mathworks.com/help/matlab/ref/fprintf.html

Here is how you can do it:

Prepare Your Matrix: Ensure that your matrix is defined in MATLAB.

   matrix = [1.93e3, 1.32e-2; 1.94e3, 1.03e-3];

Open a File for Writing: Use the fopen function to create or open a text file where you want to save your data.

   fileID = fopen('output.txt', 'w'); % Open file for writing

Format and Write Your Data: Use fprintf to write the data into the file in your desired format. You can specify the format string to ensure that numbers are represented as you wish (fixed-point).

   fprintf(fileID, '%.0f %.4f\n', matrix'); 
   % Transpose matrix for correct orientation

In this example:

%.0f formats the first column as an integer.
%.4f formats the second column with four decimal places.

Close the File: After writing, always close the file to ensure all data is saved properly.

   fclose(fileID);

Here is how your complete MATLAB code would look:

% Define your matrix
matrix = [1.93e3, 1.32e-2; 1.94e3, 1.03e-3];
% Open a text file for writing
fileID = fopen('output.txt', 'w');
% Write the matrix to the file in the desired format
fprintf(fileID, '%.0f %.4f\n', matrix');
% Close the file
fclose(fileID);

Please see attached.

Using fprintf gives you flexibility not only in formatting but also in controlling how many digits appear after the decimal point and whether numbers are displayed in fixed-point or scientific notation. By following these steps, you should be able to save your matrix in MATLAB exactly as you require!

Hope this helps.

  2 件のコメント
Holger
Holger 2024 年 11 月 4 日 9:00
Hi @Umar
thanks a lot, that helps me!
best regards
Holger
Umar
Umar 2024 年 11 月 5 日 2:46
Hi @Holger,
Thank you for your kind words. I am pleased to hear that the information provided was helpful to you. If you have any further questions or need additional assistance, please do not hesitate to reach out.

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

カテゴリ

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

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by