how to write to one decimal point from matlab to excel

조회 수: 9 (최근 30일)
yojitha etikala
yojitha etikala 2022년 2월 7일
댓글: yojitha etikala 2022년 2월 11일
Hello Guys,
I need to write the values to excel from matlab script with only one decimal point.
This is the code I used for exporting data to excel:
final_result = {Power;Power1;Power2;Power_percent};
if datasave==1
UpdateSpreadsheet(final,Spreadsheet_name,Tab_name)
end
function UpdateSpreadsheet(final, Spreadsheet_name, Tab_name)
ReadData = xlsread(Spreadsheet_name,Tab_name);
[Rows,Columns]=size(ReadData);
n= Columns+2;
xlsb = mod(n-1,26)+1 ;
xmsb = fix((n-1)/26) ;
%// find MSB (recursively if necessary)
if xmsb >= 1
col = [excel_column(xmsb) char(xlsb+64)] ;
else
col = char(xlsb+64) ;
end
cellReference = sprintf('%s1', col);
xlswrite(Spreadsheet_name, final, Tab_name, cellReference);
end
For example;
In excel it is showing like below:
Power 113.2790977
Power1 65.85196443
Power2 80.54553664
Power_percent 92.23339083
But I need the output to be:
Power 113.2
Power1 65.8
Power2 80.5
Power_percent 92.2
Can you guys help me with this?
Thanks in advance

답변 (2개)

Voss
Voss 2022년 2월 8일
You could convert those numbers to strings before writing, if you don't mind rounding to one decimal place rather than truncating:
xlswrite(Spreadsheet_name, cellfun(@(x)sprintf('%.1f',x),final,'UniformOutput',false), Tab_name, cellReference);
Or you can set the cells' NumberFormat directly, along these lines:

Image Analyst
Image Analyst 2022년 2월 8일
What if you round your array with round before you send it to Excel?
array = round(array, 1);
xlswrite(filename, array);
  댓글 수: 1
yojitha etikala
yojitha etikala 2022년 2월 11일
Hello,
For example I have my array as
distance=20
speed=27
time=1200
power=34.4
result= [distance; speed; time; power]
finalresult=round(result,1)
when I do this it is showing me this below error, because the array is not values
ERROR: Error using round
First argument must be a numeric, logical, or char array.
So, it should read these values and round them off before exporting the values to excel.
how to do this? In my script , it is not the numbers in the array to round them off
Thanks in advance

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by