MATLAB Answers

readtable error "Index exceeds array bounds." for Matlab 2018a

14 views (last 30 days)
I recently installed Matlab 2018a on my Windows 7 pc after using Matlab 2017b. In order to read a simple text file without a file extension, which can be found in the attachments I used this line of code using the readtable function:
readtable([filename], 'CommentStyle', 'Q_cbl')
I added the 'CommentStyle', 'Q_cbl' because I want Matlab to ignore the couple of lines which start with Q_cbl
This worked well enough in Matlab 2017b, until I started using Matlab 2018a. From then on I got the following error message:
Error using readtable (line 197)
Index exceeds array bounds.
Note: readtable detected the following
parameters:
'Delimiter', '\t ',
'MultipleDelimsAsOne', true,
'ReadVariableNames', false, 'Format',
'%f%q%f%f%f%f%f%f'"
I tried several things as skipping the first line with the headers, using 'HeaderLines', ignoring the # symbol using 'TreatAsEmpty' and I checked the documentation of the function, but I cannot seem to find what has changed to cause this problem. I am planning to return to Matlab 2017b, but I am worried that this problem will reoccur in later versions of Matlab.
Could you help me find a solution to this problem, or is this maybe a bug of Matlab 2018?
Edit: added .zip file with the file I am trying to read

  0 Comments

Sign in to comment.

Accepted Answer

Ruben Schulte
Ruben Schulte on 23 Jul 2018
The problem was solved thanks to the quick response of Liam from Mathworks support.
He suggested the following:
opts = detectImportOptions('initial_chem','FileType','text',...
'CommentStyle',' ');
opts.VariableTypes = {'char','char','char','char', ...
'double','double','double', 'double'};
t = readtable('initial_chem',opts);
I added his full solution with comments in the attachments in case anyone else comes acros this issue

  0 Comments

Sign in to comment.

More Answers (1)

Harsh
Harsh on 20 Jul 2018
You can add a '.txt' extension to the file as 'initial_chem.txt' and then do the following in R2018a:
>> opts = detectImportOptions('initial_chem.txt');
>> opts.DataLines = [2,30];
>> readtable('initial_chem.txt',opts)
You can configure the importOptions (opts) properties to suit your workflow. For example, specifying the 'VariableTypes'.
Alternatively, you can also use the Import Tool (Import Data button) directly on the file without the extension (initial_chem) and generate a script. You can configure properties of your choosing before importing. For example, on this file, Import as a Delimited file with Column Delimiters as (Tab, Space) & Range as A2:H31.

  1 Comment

Ruben Schulte
Ruben Schulte on 23 Jul 2018
Thanks for your suggestion!
Unfortunately, Matlab did not recognize my file when I add the .txt extension. As a result, the problem was not solved. I do like the DataLines option. I looked for that option earlier, but could not find it before.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by