2020a readtable error when specifying rectangular range
8 次查看(过去 30 天)
显示 更早的评论
I am trying to read a .xlsx file with readtable specifying a rectangular range. I get the error shown below. I have tried reading other .xlsx files with the same result. When I do not specificy the rectangular range or when I specify only the starting cell it reads the .xlsx file OK.
ChTableXLS is a string with a path to the .xlsx file
Below is a screen shot of the .xlsx sheet I am trying to read. It is an example of a longer file - only 200 rows.
Is this a known bug in 2020a readtable? I did not find it in the bug list.
I know I can read the full sheet in and only keep what I need as a work around, but the options should work. Perhaps I am doing something wrong? I don't see anything wrong in the .xlsx files I tried.
Thank you
dat = readtable(ChTableXLS,'Sheet','Sheet3','Range','C1:F4','PreserveVariableNames',true);
Error using readtable (line 198)
First input must be either a character vector or a string scalar.
0 个评论
采纳的回答
Stephen23
2022-6-8
编辑:Stephen23
2022-6-8
'DataRange','C2:F4', 'VariableNamesRange','C1:F1'
10 个评论
dpb
2022-6-8
"I will have to upgrade to the most current B version"
NB: There's nothing particularly magic about -b versu -a; I just limit the pain/time consumed to go to a new version to no more than annually.
I just mentioned it here because I hadn't seen the symptom -- and it was apparently an aberration that occurred with the R2020a version; I had used the table extensively prior to it and don't recall ever noticing or having the problem. I just don't have any earlier releases installed against which to check.
IOW, that I hadn't seen this issue was pure luck only...
dpb
2022-6-9
"there are FEX submissions that have modified xlswrite to not close the ActiveX connection automatically, but to open the file/create the connection first, then do all the output writes and close the connection/save the workbook when done. "
This feature would be a most welcome enhancement to the writeXXX class of functions; it would be ideal if the ActiveX session handle could be a persistent variable internally and there be another optional named parameter to let one control the Open/Close status programmatically. Does add a layer of UI complexity and the onus upon the user code to ensure proper synchronization/use, but would be HUGE in potential performance gains -- plus, if had access to the handle, one could then do other customizations at the same time.
更多回答(2 个)
dpb
2022-6-8
The error isn't anything to do with the 'Range' argument; it's the file name ("First input must...")
"ChTableXLS is a string with a path to the .xlsx file"
It's almost guaranteed to be a cellstr variable, then. While this is an annoyance and I fail to see why TMW doesn't expand the input parsing to handle it, the input file name must either be the dereference cellstr variable content or a string variable, NOT a cellstr() variable.
dat = readtable(ChTableXLS{:},'Sheet','Sheet3','Range','C1:F4','PreserveVariableNames',true);
or
dat = readtable(string(ChTableXLS),'Sheet','Sheet3','Range','C1:F4','PreserveVariableNames',true);
will either work, I'll betcha'....
VB ABQ
2022-6-8
1 个评论
dpb
2022-6-8
I've never experienced such a situation -- looks like the error message may need some fixup to reflect the actual problem.
To debug this would need the file -- attach the .xls file using the paperclip icon.
Just for satisfying curiosity, what does
whos ChTableXLS
return?
Have you tried
dat = readtable(ChTableXLS,"Sheet","Sheet3","Range","C2:F4");
? Just to see if a string vis a vis char() makes any difference -- wouldn't expect to.
Has the range actually had anything entered in it? I rarely use ranges on reading preferring to just clean up later; when I do, it's almost always just to limit a column range rather than a preset rectangular range.
If I do have such specialized kinds of requirements I almost always end up using an import options object and any such range would end up being defined there.
But, I have used the syntax on the rare occasion and have never seen the symptom so 'tis a puzzle off top of head, yes.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!