How to stop readcell function from round numbers in matlab?
Show older comments
I have a spreadsheet that I am trying to read and the numbers have 6 decimal places but for some reason the output is always only to 5 and then followed by zeros. For example I have the number 14.520014 but the output from readcell will only show as 14.520010. Not sure how to fix this.
2 Comments
Krzysztof Czehowicz
on 19 Jun 2020
I noticed that readcell() reduced the number of digits, like you describe. At the same time readtable() keeps the precision. I have found a not so nice looking workaround:
Data=table2cell(readtable(File,'Sheet','Data','Range','A1:BA1011'));
Philip M
on 8 Feb 2021
This didn't work for me because my spreadsheet isn't a simple MxN matrix, but I found a solution.
I guess because of the empty cells in my spreadsheet, readtable doesn't read the first row of data even when setting 'ReadVariableNames' and 'HeaderLines' to 0, so readcell must be used:
>> RC=readcell(filepath,'sheet',1)
RC =
10×4 cell array
{'Group1' } {'Var1' } {'1E4' } {1×1 missing}
{1×1 missing} {'Var2' } {[ 5]} {1×1 missing}
{1×1 missing} {1×1 missing} {1×1 missing} {1×1 missing}
{'Group2' } {'Var3' } {'AAA' } {'DDD' }
{1×1 missing} {'Var4' } {'BBB' } {'EEE' }
{1×1 missing} {'Var5' } {'CCC' } {1×1 missing}
{1×1 missing} {1×1 missing} {1×1 missing} {1×1 missing}
{'Group3' } {'Var6' } {[ 5]} {1×1 missing}
{1×1 missing} {'Var7' } {[ 0.12346]} {1×1 missing}
{1×1 missing} {'Var8' } {[ 0.01]} {1×1 missing}
The actual value of Var7 is 0.123456789, but the precision was lost. However, the precision is maintained when using readmatrix
>> nums=readmatrix(filepath,'sheet',1,'NumHeaderLines',0)
nums =
NaN NaN 5 NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN 5 NaN
NaN NaN 0.123456789 NaN
NaN NaN 0.01 NaN
The precision was maintained, but you'll notice the first row of nums is the first row of the spreadsheet that contains a number rather than the actual first row of the spreadsheet. To fix that:
>> nums=[nan(size(RC,1)-size(nums,1),size(RC,2));nums]
nums =
NaN NaN NaN NaN
NaN NaN 5 NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN 5 NaN
NaN NaN 0.123456789 NaN
NaN NaN 0.01 NaN
Finally, specify which cells to replace with the unrounded values and replace them. The first term in logi prevents numbers stored in the spreadsheet as text ("1E4" in this example, which is entered into Excell as '1E4) from being converted to a number.
>> logi=cellfun(@(C)isnumeric(C),RC) & ~isnan(nums);
>> RC(logi)=num2cell(nums(logi))
RC =
10×4 cell array
{'Group1' } {'Var1' } {'1E4' } {1×1 missing}
{1×1 missing} {'Var2' } {[ 5]} {1×1 missing}
{1×1 missing} {1×1 missing} {1×1 missing } {1×1 missing}
{'Group2' } {'Var3' } {'AAA' } {'DDD' }
{1×1 missing} {'Var4' } {'BBB' } {'EEE' }
{1×1 missing} {'Var5' } {'CCC' } {1×1 missing}
{1×1 missing} {1×1 missing} {1×1 missing } {1×1 missing}
{'Group3' } {'Var6' } {[ 5]} {1×1 missing}
{1×1 missing} {'Var7' } {[0.123456789]} {1×1 missing}
{1×1 missing} {'Var8' } {[ 0.01]} {1×1 missing}
It's messy but it seems to work reliably, at least in my case.
For convenience, here's a function that will perform the operations above:
function out=readsheet(filepath,sheetno)
out=readcell(filepath,'sheet',sheetno);
nums=readmatrix(filepath,'sheet',sheetno,'NumHeaderLines',0);
nums=[nan(size(out,1)-size(nums,1),size(out,2));nums];
logi=cellfun(@(C)isnumeric(C),out) & ~isnan(nums);
out(logi)=num2cell(nums(logi));
end
Answers (0)
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!