How to set a specified number of digits when exporting to excel?
30 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Khanh
am 4 Okt. 2014
Kommentiert: Image Analyst
am 12 Nov. 2021
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
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 4 Okt. 2014
Bearbeitet: Azzi Abdelmalek
am 4 Okt. 2014
a=[101.13123124,99.329040,201.329524,2094.302785]
b=arrayfun(@(x) sprintf('%10.4f',x),a,'un',0)
xlswrite('file.xlsx',b)
Weitere Antworten (1)
Image Analyst
am 4 Okt. 2014
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 Kommentare
karthik tharanisingh
am 12 Nov. 2021
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
am 12 Nov. 2021
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.
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!