Why I receive an error when my code is in function but not when he's in editor ?

I want to decrease my size of code, i created different function for this. But I receive a problem in one function.
This is my code and it's works when it's in editor of matlab :
for i = 1:size(Z(j).ContractionsExt_Interp,1)
pat = "visible";
NumContraction(i) = contains(visibilite(i),pat);
if NumContraction(i) == 0
Z(j).ContractionsExt_Interp_Select(i,:) = [];
else
end
end
But if I put him in my function i receive this error message: "Matrix index is out of range for deletion." for the ligne of deletion Z(j).ContractionsExt_Interp_Select(i,:) = [];
I don't understand why and how fix it. Somebody can help ?

4 Commenti

"This is my code and it's works when it's in editor of matlab : "
So, the code runs without an error and provides an input in the editor but gives an error when you run it as a function?
How do you call the function?
Could you attach the code and the data used, so that we can reproduce the error and provide any suggestions accordingly?
The error is straight-forward, however, if it occurs in one instance and not in another, then the issue could also be something else.
With this line of code
Z(j).ContractionsExt_Interp_Select(i,:) = [];
you are changing the size of the Z(j).ContractionsExt_Interp_Select variable. However your for loop acts as if it is not changed. With the limited info I have about your code and variables, I believe the issue is when you run the code from editor, you never enter the if statement but when you run it from function you get inside the if statement. Hence after sometime i becomes larger than the size of your Z(j).ContractionsExt_Interp_Select variable.
Try to change your for loop to be
for i = size(Z(j).ContractionsExt_Interp,1):-1:1
and see if this helps.
"I don't understand why ..."
The problem is very simple: you are trying to access elements of an array that do not exist.
Lets consider this simple example vector with 4 elements:
V = 1:4
V = 1×4
1 2 3 4
Now lets do exactly what a FOR loop would do looking at each element from 1...4:
V(1)
ans = 1
V(2)
ans = 2
Lets randomly remove the 2nd element:
V(2)=[]
V = 1×3
1 3 4
Question: how many elements does the vector have now? While you consider that, lets continue with the FOR loop:
V(3)
ans = 4
V(4)
Index exceeds the number of array elements. Index must not exceed 3.
Does the vector have 4 elements? (hint: no). We get an error when we try to access an element that does not exist.
"... and how fix it."
Loop over the array in reverse.
Thank you it works !!

Accedi per commentare.

 Risposta accettata

Loop in reverse would fix the issue (cross interaction between deletion that makes array strinks and loop index)
for i = size(Z(j).ContractionsExt_Interp,1):-1:1 % change here
pat = "visible";
NumContraction(i) = contains(visibilite(i),pat);
if NumContraction(i) == 0
Z(j).ContractionsExt_Interp_Select(i,:) = [];
else
end
end

Più risposte (1)

You could consider to vectorize the test instead of using for loop and the side effect when deleting array element(s). I don't know your data class and organization, but it could go something like this;
NumContraction = contains({ visibility }, pat);
Z(j).ContractionsExt_Interp_Select(NumContraction & NumContraction(:)'==0) = [];

Categorie

Scopri di più su Environment and Settings in Centro assistenza e File Exchange

Prodotti

Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by