Index out of bounds error when working with data imported from excel

2 views (last 30 days)
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.

Accepted Answer

Paulo Silva
Paulo Silva on 21 Jan 2011
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))]';

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!