Index in position 2 is invalid. Array indices must be positive integers or logical values.

조회 수: 2 (최근 30일)
In the third line of code,
csvname = strcat(cpath, "\", cname);
c = csvread(csvname, 1, 0);
raw_data(:,i) = c(1:(min(size(c,1),max_val)),2:size(c,2));
where cpath and cname are the file path and file name of the file, I am getting this error:
Index in position 2 is invalid. Array indices must be positive integers or logical values.
I checked the csv file in question, and it is a valid file (no non-numeric values that are being read). A sample of the file is attached.
  댓글 수: 2
Stephen23
Stephen23 2019년 7월 30일
@Varun Lakshmanan: please show us the complete error message. This means all of the red text. The error message has information that helps understand the problem.
Varun Lakshmanan
Varun Lakshmanan 2019년 7월 30일
Thanks for your response. I went back and looked at my code again, and realized that the error given is on the third line in the code in my question (I updated my question with the relevant lines of code).

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2019년 7월 30일
Somehow you have managed to assign a value to a variable named csvread and that is interfering with your calling csvread as a function.
Note: if you want to skip the one line header in the file, then you should use 1, 0 instead of 2, 0 . The value is not which row number to start at: it is how many rows to skip, so to start at row 2 then you want to skip 1 row.
You should probably be considering readtable()
  댓글 수: 1
Varun Lakshmanan
Varun Lakshmanan 2019년 7월 30일
편집: Varun Lakshmanan 2019년 7월 30일
Thanks for your response. I went back and looked at my code again, and realized that the error given is on the third line in the code in my question (I updated my question with the relevant lines of code).

댓글을 달려면 로그인하십시오.


Andy
Andy 2019년 7월 30일
If the 3 lines of code are all you are running, where is i set when you attempt to reference it in line 3?
  댓글 수: 2
Varun Lakshmanan
Varun Lakshmanan 2019년 7월 30일
I'm running more than 3 lines of code, but this is the necessary code snippet to understand the problem. i should be set in line 3, as I'm not receiving any runtime or compile errors that the use of i is undefined in line 3.
Walter Roberson
Walter Roberson 2019년 7월 30일
By default i is a function that returns sqrt(-1) so you will never get an error about i or j being undefined.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by