xlsread returns NaN for cell references to string
Show older comments
I'm reading an excel document with xlsread with references to other cells that be either text or numeric values. The problem is that xlsread seems to interpret cells starting with an "=" as numeric even if is a reference to a cell with text. Thus the result of a reference to text cell returns NaN, even in the raw format. Is there any way to work around this? I've tried some tricks (see below) that forces Excel to treat the cell as text but it doesn't work with xlsread. The (very) simple example below illustrates the issue. Example:
A B C D
1 My age is 10
2 =A1 =B1&"" =TEXT(C1;0) =D1
> [num text raw] = xlsread('book1.xls')
num =
NaN NaN NaN 10
NaN NaN NaN 10
text =
1×3 cell array
'My' 'age' 'is'
raw =
2×4 cell array
'My' 'age' 'is' [10]
[NaN] [NaN] [NaN] [10]
Matlab 2016b, Excel 2010 (xls format), Linux.
9 Comments
jean claude
on 12 Oct 2017
Anders Bergåker
on 12 Oct 2017
Fangjun Jiang
on 12 Oct 2017
For your information, I tried this:
MATLAB R2016b
Windows 7
Excel 2010
Excel file saved as .xls (Excel 97-2003 Workbook)
The cell is read as its value, not NaN. For example, my cell C1 is "=SUM(A1:B1)" but it was read as its numeric value.
Anders Bergåker
on 12 Oct 2017
Image Analyst
on 12 Oct 2017
If it's a string (enclosed in double quotes), it's a string, and will/should return a string. If you get rid of those double quotes, it will treat it as a formula and should return the numbers, like Fangjun got.
Anders Bergåker
on 13 Oct 2017
Fangjun Jiang
on 13 Oct 2017
FYI, with your example file, xlsread() returns value, not NaN or formula. Maybe you should report this to the Mathworks tech support.
>> [num, text, raw] = xlsread('test.xls')
num =
[]
text =
1×2 cell array
'Hello' 'Hello'
raw =
1×2 cell array
'Hello' 'Hello'
Walter Roberson
on 13 Oct 2017
The behavior is different in basic mode, which is for any system that is not windows with excel installed. The poster mentioned basic mode and Linux.
Anders Bergåker
on 16 Oct 2017
Accepted Answer
More Answers (0)
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!