I am reading a spreadsheet into MATLAB using 'xlsread', but for some reason the last few significant digits are incorrect.
For example, when I look at a cell in Excel, its value is 0.625. However, after importing with 'xlsread' in MATLAB, I am seeing:
>> sprintf('%.16f', cellValue)
ans =
'0.6254000000000002'
Why does this happen?

 採用された回答

MathWorks Support Team
MathWorks Support Team 2017 年 6 月 6 日

0 投票

The inconsistency described here is due to the fact that in some cases the displayed values in Excel can vary from the exact internal value of a cell.
To make the imported values match those seen in the Excel Spreadsheet, you can either round the true values within Excel, or you can round the imported values in MATLAB. To automatically round the values imported from 'mySheet.xlsx', you can specify a process function when calling 'xlsread':
[d,headers,raw,custom] = xlsread('mySheet.xlsx','','', '', @processFcn);
where 'processFcn' is defined as:
function [data,rounded] = processFcn(data)
rounded = round(cell2mat(data.Value), 4);
end
In the example above, the 'custom' output argument will contain the rounded values from your spreadsheet.
 

その他の回答 (0 件)

製品

リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by