Error: Index in position 1 is invalid. Array indices must be positive integers or logical values

2 views (last 30 days)
I am having trouble with the script. The error: Index in position 1 is invalid. Arrays indices must be positive integers or logical values. What does that mean? I am really sorry, I am a novice and this a very old script which I am using for my current study.
data_final = xlsread ( 'Raw_data_2.xls' );
% get number of trials
ntrials = length ( 'code' )
for i = 1: ntrials
temp_data = data_final (find (data_final (:, 2) == i), :);
t_len = temp_data (end, 1) +1;
trial_data (i) = .t_len t_len;
if (size (temp_data, 1) ~ = t_len)
x = sprintf ( 'trial% d:% d record (s) lost' , i, t_len-size (temp_data, 1));
disp (x)
end
T_interv = unique (temp_data (2: end, 4) '- temp_data (1: end-1,4)');
trial_data (i) .step = min (temp_data (2: end, 4) '- temp_data (1: end-1,4)');
trial_data (i) .time (1: t_len) = NaN;
trial_data (i) .time (temp_data (:, 1) '+ 1) = 1000 * (temp_data (:, 4)' - temp_data (1,4));
trial_data (i) .xdat (1: t_len) = NaN;
trial_data (i) .xdat (temp_data (:, 1) '+ 1) = temp_data (:, 8)';
trial_data (i) .psize (1: t_len) = NaN;
trial_data (i) .psize (temp_data (:, 1) '+ 1) = (temp_data (:, 7) ./ PIXTODEG.x)';
trial_data (i) .xpos (1: t_len) = NaN;
trial_data (i) .xpos (temp_data (:, 1) '+ 1) = (temp_data (:, 5) ./ PIXTODEG.x)';
trial_data (i) .ypos (1: t_len) = NaN;
trial_data (i) .ypos (temp_data (:, 1) '+ 1) = (temp_data (:, 6) ./ PIXTODEG.y)';
end
eye = struct ( 'ntrials' , ntrials, 'eye' , trial_data);

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 3 Dec 2019
Edited: KALYAN ACHARJYA on 3 Dec 2019
Here is the issue? In first iteration, when i=1;
temp_data=data_final(find(data_final(:,2)==i),:);
Does not find any data, which fulfil the condition "data_final(:,2)==i", here for 1st iteration, the condition is data_final(:,2)==1, hence it return the empty data aray ([ ]), i.e. after the line execute temp_data=[ ];
Next line, when the code execute to access the empty data at the following line
t_len=temp_data(end,1)+1;
%................^
here you are trying to acess the temp_data as 2D array. temp_data(end,1)>> Try to acess the data element from temp_data which having last row & 1 column, which actually doesnot have, hence it reflects error
>> temp_data(end,1)
Subscript indices must either be real positive integers or logicals.
More examples:
  1. temp_data as non empty (non error)
>> temp_data=[3 4 5]; % Just an example
>> t_len=temp_data(end,1)+1
t_len =
4
2. temp_data as empty (error)
>> temp_data=[];
>> t_len=temp_data(end,1)+1
Subscript indices must either be real positive integers or logicals.
If temp_data is non empty
Solutions: Ensure that temp_data return non empty.
Hope this have sufficient clues to get rid off the error.

More Answers (1)

Image Analyst
Image Analyst on 3 Dec 2019
Edited: Image Analyst on 3 Dec 2019
This is a FAQ, so see the FAQ
Also, I don't believe you can have a space between the structure name and the field name like here:
trial_data (i) .xdat (1: t_len) = NaN;
^ Why is there a space here???

Community Treasure Hunt

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

Start Hunting!