2020a readtable error when specifying rectangular range

4 views (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 8 Jun 2022
Edited: Stephen23 on 8 Jun 2022
'DataRange','C2:F4', 'VariableNamesRange','C1:F1'
  10 Comments
dpb
dpb on 8 Jun 2022
"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
dpb on 9 Jun 2022
"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.

Sign in to comment.

More Answers (2)

dpb
dpb on 8 Jun 2022
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'....
  1 Comment
VB ABQ
VB ABQ on 8 Jun 2022
Thanks, but readtable works fine when I omit Range. This statement works fine.
I found readcell does the same thing, BTW
dat = readtable(ChTableXLS,'Sheet','Sheet3','PreserveVariableNames',true);

Sign in to comment.


VB ABQ
VB ABQ on 8 Jun 2022
Thanks for the idea, but it did not work :-(
But specifying only the 1st .xlsx cell 'C2' is OK
>> dat = readtable(ChTableXLS,'Sheet','Sheet3','Range','C2:F4');
Error using readtable (line 198)
First input must be either a character vector or a string scalar.
>> dat = readtable(ChTableXLS,'Sheet','Sheet3','Range','C2');
  1 Comment
dpb
dpb on 8 Jun 2022
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.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!