MATLAB: Is xlsread reading incorrect values

incorrectMATLABprecisionroundvaluesxlsread

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?

Best Answer

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.