index out of bounds

1 view (last 30 days)
Susana
Susana on 17 Jul 2013
I am trying to run a script and I am getting the following error:
??? Attempted to access juju(9); index out of bounds because numel(juju)=8.
Error in ==> RejectandConfirmArtifacts at 58 Trl_p = [Confirm.cfg.trl(juju(c)+1:juju(c+1),1:3)];
Here is the piece of code where the error appears:
juju=[]; for i = 1:length(Confirm.cfg.trl)-1 if Confirm.cfg.trl(i,1) > Confirm.cfg.trl(i+1,1) juju (end+1, :) = i; end end juju = [0;juju;length(Confirm.cfg.trl)];
for c = 1:info.conditions.number
Trl_p = [Confirm.cfg.trl(juju(c)+1:juju(c+1),1:3)];
Trl{c} = Trl_p;
end
clear Trl_p
Trl_sem_erros = Trl; clear Trl
Here is the entire code:
CONFIRM BAD TRIALS - CHANNEL METHOD
%--------------------------------------------------------------------------
clear all
InfoSubjects
InfoExperiment
[rw,cl] = size (Infosubjects); clear cl
for j = 1:rw
%----------------------------------------------------------------------
% LOAD EVENTS AND TRL
%----------------------------------------------------------------------
load(sprintf('%s%s%s',info.expname,Infosubjects{j,2},'\EVENTS.mat'));
load(sprintf('%s%s%s',info.expname,Infosubjects{j,2},'\TRL.mat'));
load(sprintf('%s%s%s',info.expname,Infosubjects{j,2},'\ERROS.mat'));
load(sprintf('%s%s\\%s',info.expname,Infosubjects{j,2},Infosubjects{j,4},'\Artifacts_total.mat'));
hdr = ft_read_header(sprintf('%s%s\\%s\\%s%s',info.expname,Infosubjects{j,2},Infosubjects{j,3},Infosubjects{j,2},info.filename));
%----------------------------------------------------------------------
% CONFIRM BAD TRIALS AND FIND BAD CHANNELS
%----------------------------------------------------------------------
Trl = Trl_sem_erros;
cfg = [];
cfg.dataset = sprintf('%s%s\\%s\\%s%s',info.expname,Infosubjects{j,2},Infosubjects{j,3},Infosubjects{j,2},info.filename);
Trl_p = []; cfg.trl = [0 0 0];
for c = 1:info.conditions.number
Trl_p = Trl{c}; cfg.trl = [cfg.trl;Trl_p];
end
clear Trl_p
cfg.trl = cfg.trl(2:length(cfg.trl),:); cfg.channel = hdr.label(1:info.nC);
cfg.blc = 'yes';
cfg.reref = 'yes'; cfg.implicitref = []; cfg.refchannel = info.channel.ref;
Confirm_p = preprocessing(cfg);
cfg = []; cfg.method = 'channel'; Confirm_Channel = rejectvisual(cfg, Confirm_p);
juju=[];
for i = 1:length(Confirm_Channel.cfg.trl)-1
if Confirm_Channel.cfg.trl(i,1) > Confirm_Channel.cfg.trl(i+1,1)
juju (end+1, :) = i;
end
end
juju = [0;juju;length(Confirm_Channel.cfg.trl)];
for c = 1:info.conditions.number
Trl_p = [Confirm_Channel.cfg.trl(juju(c)+1:juju(c+1),1:3)];
Trl{c} = Trl_p;
end
Can some one help me to fix it? Thanks a lot! Susana

Answers (3)

Iain
Iain on 17 Jul 2013
juju defines the intervals between sections. With 8 intervals, you define 7 sections. It fails when trying to get the 8th section. If there is a defining boundary (I guess at either the start, or the end), you'll need to add that boundary to juju:
Eg:
juju = [start_boundary juju];
juju(end+1) = end_boundary;
  3 Comments
Iain
Iain on 17 Jul 2013
That depends on what you're doing.
If I wanted to split "My name is Earl and I believe in karma" into words, and I had the index of each space, "juju", I'd need to set start_boundary to 1, and end_boundary to the length of the string.
But you might have " my name is Earl and I believe in karma", or "my name is Earl and I believe in karma ", so you might only need one of them.
Iain
Iain on 17 Jul 2013
I reckon:
start_boundary = 1; %(or maybe 2...)
juju = [start_boundary juju];

Sign in to comment.


dpb
dpb on 17 Jul 2013
for c = 1:info.conditions.number
Trl_p = [Confirm_Channel.cfg.trl(juju(c)+1:juju(c+1),1:3)];
The error message informs you that length(juju)==8
From the above one must presume that info.conditions.number is also 8; one presumes that perhaps that's how it was defined as that length; the neceassary code to see that isn't shown.
But, given the above presumption, the expression juju(c+1) will always try to address an element one beyond the length of juju.
It would appear that the loop index should run from
for c = 1:info.conditions.number-1
to keep the address in bounds.
  2 Comments
Susana
Susana on 17 Jul 2013
Edited: Susana on 17 Jul 2013
yes, info.conditions.number is also 8 (it is defined elsewhere).
If, as suggested, I set "for c = 1:info.conditions.number-1" I no longer receive the error message. However, now my variable "Trl_sem erros" isn't correct (apparently it is correct just from columns 1 to 3).
(I don't know if an aspect that I didn't mentioned could perhaps be important...: my experiment was split into 2 lists; each list have the 8 conditions; so after the matlab have read the 8 conditions from list 1, it have to read again conditions 1 to 8 from list 2)
dpb
dpb on 17 Jul 2013
Well, only you can know what the intent of the code is as to how to compute the other variable and what to do at the boundary condition; that's part of the problem definition/specification.
As Jan says below, keeping the information around instead of wiping it all out may help in understanding what's going on and what you do want instead but clearly you've got to deal with the problem of addressing beyond the defined boundaries of the array as the present code does.
What the two lists actually have to do with the symptoms (if anything) is again impossible to say w/o knowing the objective.
You're asking for a definitive solution to a specific error but there's no context of the larger picture of what it is that is trying to be computed; only a piece of code.

Sign in to comment.


Jan
Jan on 17 Jul 2013
If you omit the useless clear all, you could use the debugger to see, what's going on inside the code. Then set a breakpoint in the first line and step through the code line by line.
Because the debugger is your friend, clear all is not.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!