Index out of bounds error when working with data imported from excel
3 次查看(过去 30 天)
显示 更早的评论
I have this problem working with data I imported from excel. It did not import the way I expected. My code is like this
clear all, close all, clc
data=xlsread('Test-MATLAB.xls',-1);
data(:,5) = -99999*ones(10,1);
depth=[1:1:max(data(:,1))]';
n=numel(depth);
m=4;
for j=1:m%
for i=1:n
idx=find(data(:,1)==depth(i));
if numel(idx)==0;
data2(i,j)=-99999;
else
data2(i,j)=data(i,j);
end
end
end
data2
xlswrite('Test-MATLAB.xls',data2,'Daten2','B2')
This is the Test-MATLAB.xls
depth x y z
2 5 3 8
4 5 3 8
7 5 3 8
9 5 3 8
12 5 3 8
13 5 3 8
18 5 3 8
19 5 3 8
23 5 3 8
28 5 3 8
When I executed it the code with this data I get this error:
??? Attempted to access data(12,2); index out of bounds because size(data)=[10,5].
Error at 14
data2(i,j)=data(i,j+1);
any idea?
I also want to be able to save the data to excel when I am done.
0 个评论
采纳的回答
Paulo Silva
2011-1-21
I believe that the error is in the line
depth=[1:1:max(data(:,1))]';
because
max(data(:,1))
will give you the value 28 and you don't have 28 lines in your data, maybe the correct version of that line is
depth=[1:numel(data(:,1))]';
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!