Error: "index in position 2 exceeds array bounds"
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
KALYAN ACHARJYA
el 1 de Ag. de 2020
Editada: KALYAN ACHARJYA
el 1 de Ag. de 2020
Please attach mentioned files.
Respuestas (2)
KALYAN ACHARJYA
el 1 de Ag. de 2020
Editada: KALYAN ACHARJYA
el 1 de Ag. de 2020
Temp = DOdata.data(:,3)
DOdata.data have only 2 columns, you are trying to access 3rd column.
Walter Roberson
el 1 de Ag. de 2020
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.
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!