フィルターのクリア

How to set a specified number of digits when exporting to excel?

34 ビュー (過去 30 日間)
Khanh
Khanh 2014 年 10 月 4 日
コメント済み: Image Analyst 2021 年 11 月 12 日
Hi,
I have an array
a=[101.13123124,99.329040,201.329524,2094.302785];
Could someone tell me how to write the array to excel with 4 digits as I set format short in the command window?
As
a=[101.1312,99.3290,201.3295,2094.3027];
How about other number of digits such as 3 digits.
My code:
clc
clear all
format short
filename=input('Name file output: ','s');
fileadd=strcat('c:\',filename,'.xlsx');
a=[101.13123124,99.329040,201.329524,2094.302785];
xlswrite(fileadd,a,1,'A6');
Thanks

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 10 月 4 日
編集済み: Azzi Abdelmalek 2014 年 10 月 4 日
a=[101.13123124,99.329040,201.329524,2094.302785]
b=arrayfun(@(x) sprintf('%10.4f',x),a,'un',0)
xlswrite('file.xlsx',b)
  1 件のコメント
Khanh
Khanh 2014 年 10 月 5 日
Thank you so much. My problem was solved.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 10 月 4 日
Rather than throwing away precision, I'd recommend using either ActiveX, where you can tell Excel to set the number of decimal places to whatever you want, or just have a template with that precision set up in advance. If you want to learn ActiveX, I've attached a demo, though this is a simple demo so I don't have code in there to set the number of decimal points (available upon request).. If you think it's too advanced for you, then use Azzi's method which does exactly what you asked for.
  3 件のコメント
karthik tharanisingh
karthik tharanisingh 2021 年 11 月 12 日
Hello @Image Analyst. I just happened to fall onto this thread and I am looking for a way to round the values in Excel though ActXserver. I went through your code and generally gave me a great insight into how to use ActXserver. I would be interested in knowing about the rounding as well.
Image Analyst
Image Analyst 2021 年 11 月 12 日
You can round the displayed values by setting the number of decimal points. I have a function for that in my attached class.
Otherwise you can use the MATLAB round() function to round them before sending to Excel.

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

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by