saving in a specific text file format

Dear Matlab users
I am preparing a text file. I have an excel file (A.xlsx) with two columns.
25.000 10.000
26.000 9.000
......... ..........
I want to save a text file with the following format(first number,second number with three decimal).
25.000,10.000
26.000,9.000
I tried the code like this. But, the output result comes out with zigzag and not a separated files. So, how can I save the data and please help me. Thank you very much.
clear all;clc
A= xlsread('A.xlsx');
A1=A(:,1);
A2=A(:,2);
fileID = fopen('output.txt','w');
fprintf(fileID,'%2.3f,%2.3f\n',A1,A2);
fclose(fileID);

回答 (1 件)

dpb
dpb 2019 年 10 月 28 日

0 投票

>> fmt='%8.3f,%8.3f\n'
fmt =
'%8.3f,%8.3f\n'
>> fprintf(fmt,A.')
25.000, 10.000
26.000, 9.000
>>

4 件のコメント

soe thiha
soe thiha 2019 年 10 月 29 日
編集済み: soe thiha 2019 年 10 月 29 日
hello sir
how can i remove space after comma because I don't need it in my file.
Walter Roberson
Walter Roberson 2019 年 10 月 29 日
If you do not have the fixed width then your output will appear zigzag which you do not want.
soe thiha
soe thiha 2019 年 10 月 29 日
dear walter thank you for your comments.
dpb
dpb 2019 年 10 月 29 日
You can minimize the number of blanks by setting the width of the field to the minimum needed for the largest value in the array but you can't eliminate entirely unless all values are within the same order of magnitude (hence have same number of digits).

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

質問済み:

2019 年 10 月 28 日

コメント済み:

dpb
2019 年 10 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by