error using readtable : Array indices must be positive integers or logical values.

Hi,
I have a simple tab-delimited file text which looks like :
Month H_Gh H_Dh H_Bn Ta
Jan 33 17 55 0.5
Feb 52 29 60 1.4
Mar 97 51 97 5.6
Apr 123 60 113 9.7
I want to create a table with readtable(filename) Matlab function. So I have tried this directly into console
T = readtable('data.txt', 'Delimiter', '\t')
Error using readtable (line 197)
Array indices must be positive integers or logical values.
Note: readtable detected the following parameters:
'HeaderLines', , 'ReadVariableNames', true, 'Format', ''
I have tried with others text files. If all rows in the file are the same types, readtable works well, but if a line contains string and numbers (like my file), this error pop. Anyone have an solution to propose ?
I'm on windows7 with R2018a.

2 件のコメント

It works on this forum using your sample file:
T = readtable('simpleData.txt', 'Delimiter', '\t')
T = 12×5 table
Month H_Gh H_Dh H_Bn Ta _______ ____ ____ ____ ____ {'Jan'} 33 17 55 0.5 {'Feb'} 52 29 60 1.4 {'Mar'} 97 51 97 5.6 {'Apr'} 123 60 113 9.7 {'May'} 161 91 115 13.7 {'Jun'} 178 79 160 18 {'Jul'} 176 76 165 19.3 {'Aug'} 153 67 148 18.7 {'Sep'} 105 52 102 14.5 {'Oct'} 63 42 51 10.2 {'Nov'} 33 18 49 4.9 {'Dec'} 24 16 32 1.2
Arthur
Arthur 2022 年 8 月 5 日
Indeed, that works on this forum by running with matlab licence R2022a, but i'm developing with R2018a.When I run it on command window the error mentioned above pops.

サインインしてコメントする。

回答 (1 件)

Mathias Smeets
Mathias Smeets 2022 年 8 月 4 日
This seems to work:
opts = detectImportOptions('simpleData.txt');
tbl = readtable('simpleData.txt',opts);
I hope this is what you are asking :)

7 件のコメント

Arthur
Arthur 2022 年 8 月 5 日
編集済み: Arthur 2022 年 8 月 5 日
Thanks for this proposal. When I run first line
>> opts = detectImportOptions('C:\tmp\simpleData.txt');
Array indices must be positive integers or logical values.
Error in matlab.io.spreadsheet.internal.detectTypes (line 54)
types = typenames(dominateType);
Error in matlab.io.internal.text.getTextOpts (line 85)
[types, metaRows, emptyCols] = matlab.io.spreadsheet.internal.detectTypes(ids,emptyColType);
Error in detectImportOptions (line 268)
opts = matlab.io.internal.text.getTextOpts(filename,emptyColType,args);
54 types = typenames(dominateType);
I use pause on error and dominateType from matlab.io.spreadsheet.internal.detectTypes equals to 1.5 but used as an array indice.
matlab.io.spreadsheet.internal.detectTypes file is not editable
Mathias Smeets
Mathias Smeets 2022 年 8 月 5 日
編集済み: Mathias Smeets 2022 年 8 月 5 日
Try clearing your variables:
clear all
This might very maybe help
Unfortunately
>> clear all
>> opts = detectImportOptions('C:\tmp\simpleData.txt');
Array indices must be positive integers or logical values.
Error in matlab.io.spreadsheet.internal.detectTypes (line 54)
types = typenames(dominateType);
Error in matlab.io.internal.text.getTextOpts (line 85)
[types, metaRows, emptyCols] = matlab.io.spreadsheet.internal.detectTypes(ids,emptyColType);
Error in detectImportOptions (line 268)
opts = matlab.io.internal.text.getTextOpts(filename,emptyColType,args);
I consider made a function by myself to parse file...
Mathias Smeets
Mathias Smeets 2022 年 8 月 5 日
Are you sure there isn't some other function in your path that is called 'detectImportOptions' or something like that? Are you sure your code is calling the built in matlab function?
Arthur
Arthur 2022 年 8 月 5 日
When I run in debug, the detectImportOptions function called is the one situated at 'C:\Program Files\MATLAB\R2018a\toolbox\shared\io\detectImportOptions.m'. So I guess yes
Mathias Smeets
Mathias Smeets 2022 年 8 月 5 日
Very strange, I looked it up online and it seems like some people, as you can see here, solved some similar problems by reinstalling. But my suggestion would be to wait a bit until other people answer.
Sorry I can not help you further.
Arthur
Arthur 2022 年 8 月 5 日
I will try, thank you for your time

サインインしてコメントする。

カテゴリ

質問済み:

2022 年 8 月 4 日

コメント済み:

2022 年 8 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by