Error: "index in position 2 exceeds array bounds"

1 回表示 (過去 30 日間)
Matthew Warrener
Matthew Warrener 2020 年 8 月 1 日
回答済み: Walter Roberson 2020 年 8 月 1 日
I am trying to set up a basic loop for importing/processing csv files. When I run my code I get the error "index in position 2 exceeds array bounds". Can anyone tell me why this is happening?
clear all
%Insert Loop
for i=1:2
%Set file to load
if i==1
filename = 'DOaerationJuly.csv';
end
if i==2
filename = 'DOoutfallJuly.csv';
end
%Load data
delimiterIn = ',';
headerlinesIn = 1;
DOdata = importdata(filename,delimiterIn,headerlinesIn);
%Name Variables: Distance, Time, [DO]
Time = DOdata.data(:,1);
DO = DOdata.data(:,2);
Temp = DOdata.data(:,3)
plot(Time,Temp)
end
  2 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 8 月 1 日
編集済み: KALYAN ACHARJYA 2020 年 8 月 1 日
Please attach mentioned files.
Matthew Warrener
Matthew Warrener 2020 年 8 月 1 日
Thank you for responding, please see attached. These were origially acquired as html files and converted to csv files.

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

回答 (2 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 8 月 1 日
編集済み: KALYAN ACHARJYA 2020 年 8 月 1 日
Temp = DOdata.data(:,3)
DOdata.data have only 2 columns, you are trying to access 3rd column.
  1 件のコメント
Matthew Warrener
Matthew Warrener 2020 年 8 月 1 日
Thank you, can you tell me how to add a column to this? I figured it would add one by default since my imported data has three columns.

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


Walter Roberson
Walter Roberson 2020 年 8 月 1 日
Use readtable() instead of importdata()
The difficulty you are encountering is that the first column is not considered numeric, and so is not being returned as .data by importdata(), so the .data that importdata() returns has column 1 corresponding to DO and column 2 corresponding to Temp . If you use readtable() for a version within the last few years, then it will automatically detect and convert the time to datetime() and will return a total of three columns.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by