format data into string and write a txt file
古いコメントを表示
Hi,
I have the following cell,
a={[1E5 0 0 7.82E12 0]
[-1 0 0 -1 0]};
and I want to write a text file with the following format
1.0000000000000000E+05 0.0000000000000000E+00 0.0000000000000000E+00 7.8200000000000000E+12 0.0000000000000000E+00
-1.0000000000000000E+00 0.0000000000000000E+00 0.0000000000000000E+00 -1.0000000000000000E+00 0.0000000000000000E+00
As in two blank spaces at the beginning of the line if the first value is positive (line 1) and one blank space if it is negative (line 2), the "-" uses one space. There are also two blank spaces in between the values (if the value is negative the "-" uses one of those blank spaces).
First I tried converting a to a string cell:
for i=1:2
a{i}=num2str(a{i},'%.16e');
end
And I get this:
a =
2×1 cell array
{'1.0000000000000000e+050.0000000000000000e+000.0000000000000000e+007.8200000000000000e+120.0000000000000000e+00' }
{'-1.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00-1.0000000000000000e+00 0.0000000000000000e+00'}
There are no blank spaces at all. Is it possible to specify the number of blank spaces in between the values?
Thank you,
採用された回答
その他の回答 (1 件)
madhan ravi
2018 年 10 月 24 日
編集済み: madhan ravi
2018 年 10 月 25 日
a={[1E5 0 0 7.82E12 0]
[-1 0 0 -1 0]};
dlmwrite('sample.txt',a,'delimiter','\t','precision','%0.16e')
8 件のコメント
madhan ravi
2018 年 10 月 25 日
Try the updated answer
madhan ravi
2018 年 10 月 25 日
1.0000000000000000e+05 0.0000000000000000e+00 0.0000000000000000e+00 7.8200000000000000e+12 0.0000000000000000e+00
-1.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00 -1.0000000000000000e+00 0.0000000000000000e+00
madhan ravi
2018 年 10 月 25 日
the above is the format in the text file
FM
2018 年 10 月 25 日
madhan ravi
2018 年 10 月 25 日
編集済み: madhan ravi
2018 年 10 月 25 日
_it writes down both rows of a in the same line in the text file_
no there are two rows in my text file
madhan ravi
2018 年 10 月 25 日
_I try what you suggest I do get a blank space between the values_
thats how you can distinguish the values right??
madhan ravi
2018 年 10 月 25 日
see attached file
FM
2018 年 10 月 25 日
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!