xlsread returns NaN for cell references to string
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2017-10-13
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.
采纳的回答
Walter Roberson
2017-10-13
On MS Windows systems with Excel installed, it is possible to create an activexserver object to talk to excel in order to extract macro text or execute macros.
On non-Windows systems or MS Windows systems without Excel, or when 'basic' mode is specifically asked for, in order to read .xlsx files, a MATLAB routine xlsreadXLSX is called. The routine calls upon some java routines to extract portions of the .xlsx (because .xlsx are directories of .xml files that have been ZIP'd together) into text form. xml text form in hand, the MATLAB routine calls upon regexp() to parse the xml. Worst case, that code could be copied and hacked to know about formulae.
On non-Windows systems or MS Windows systems without Excel, or when 'basic' mode is specifically asked for, in order to read .xls files a MATLAB routine xlsreadBasic is called. It invokes the internal MATLAB routine biffread to do the binary read and discover the headers, but then it calls upon the built-in routine biffparse to turn the bytes into data. There is no configurability there. Handling macros would require writing or finding an xls parser. I seem to recall having encountered one in Java.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!