readtable failing on MATLAB documentation example

5 views (last 30 days)
I am trying to perform a read of a simple CSV file. When my code did not work, I decided to run the exact example provided in the MATLAB documentation. I get the exact same cryptic error message. Otherwise MATLAB seems to be working perfectly.
Matlab example .csv data, I placed in "test.txt" (I also tried with "test.csv")
LastName,Gender,Age,Height,Weight,Smoker
Smith,M,38,71,176,1
Johnson,M,43,69,163,0
Williams,F,38,64,131,0
Jones,F,40,67,133,0
Brown,F,49,64,119,0
commands run after a "clear all"
filename = "test.txt"
T = readtable(filename);
resulting MATLAB error message:
Error using readtable (line 216)
REPLACE: "what" and "by" matrices incompatible
Note: readtable detected the following parameters:
'Delimiter', ',', 'HeaderLines', 0, 'ReadVariableNames', true, 'Format', ''
I traced execution through the MATLAB's readtable.m and subsquent calls and verified that the file is opening, the header line is read, and initially the "guessed format" appears correct. After determining the data format, MATLAB raises an exception at the next higher step in the call hierarchy. I've tried quite a few variations with and without various function options/arguments.
Any idea what I am doing wrong?
  5 Comments
Image Analyst
Image Analyst on 17 Dec 2020
Mine shows
>> which -all readtable
C:\Program Files\MATLAB\R2020b\toolbox\matlab\iofun\readtable.m
C:\Program Files\MATLAB\R2020b\toolbox\shared\io\general\+matlab\+io\@ImportOptions\ImportOptions.m % matlab.io.ImportOptions method
Can you upgrade to the newest version? why stay stuck on a 2 year old version?
dpb
dpb on 18 Dec 2020
That's what R2019b shows; R2017b shows the one file; I don't have R2018b installed to check.
OP could also check to see if are updates to the release he has installed.

Sign in to comment.

Answers (3)

Brad Fennell
Brad Fennell on 17 Dec 2020
This is a new install from over a year ago. While I have used readtable on other installations successfully, I don't think I have used it on this computer. After continuing to research I found the following useful suggestion:
opts = detectImportOptions(tempdata);
t = readtable(tempdata, opts);
I just confirmed this suggestion worked both for the example and for my real data. I suggest that the current readtable has a bug (in my installation). When I was tracing it appeared the execution path resulted in the inability of the lower level function to populate the opts structure with auto detected data. I assume the workaround above successfully populates the opts structure in advance allowing the function to work properly. I don't know what is different about this installation versus those that work properly. The following is call hierarchy.
readtable.m
table.readFromFile...
table.readTextFile...
guessFormat
matlab.io.internal.text.determineFormatString(...
function val = get.VariableTypes(obj)
val = {obj.var_opts.Type};
val contains what looks like valid types with correct dimensions, however, if you try to examine
obj.var_opts.Type
the following error is raised
An unexpected error has occurred:
In class 'matlab.io.ImportOptions', the get method for Dependent property 'VariableTypes' attempts to access the stored property value. Dependent properties don't store a value and can't be accessed from their get method.
As soon as you step out, guessFormat raises an exception in the try/catch.
The good news is the suggestion to use detectImportOptions is an acceptable workaround so I am progressing once again :-)
  1 Comment
dpb
dpb on 17 Dec 2020
As Steven L, suggests, show us what
which -all readtable
shows and we still don't know which release you're running, either.

Sign in to comment.


Brad Fennell
Brad Fennell on 17 Dec 2020
which -all readtable
C:\RTN\MATLAB\R2018b\toolbox\matlab\iofun\readtable.m

Brad Fennell
Brad Fennell on 17 Dec 2020
Edited: Cris LaPierre on 18 Jun 2021
Hmm, when I posted the question it asked for the release and I thought it would be visible. Sorry. Here it is
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.5.0.944444 (R2018b)
MATLAB License Number: ******
Operating System: Microsoft Windows 10 Enterprise Version 10.0 (Build 17763)
Java Version: Java 1.8.0_152-b16 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!