Why do I get Error: Subscript indices real positive integers/logicals?

4 Ansichten (letzte 30 Tage)
I have a function that calculates the gap/overlap between two spheres. The spheres can have either the format [x,y,z,r] or [x;y;z;r], so I included some code at the beginning to bring them both to the same format. The rest is a rather simple distance calculation.
This worked fine up to now, no matter which of the two input formats I used for input. However, for some reason this has stopped working (at all, no matter which format i use) and I keep getting the message "Subscript indices must either be real positive integers or logicals."
Here is my code:
function [ distance ] = distcheck( sph1, sph2 )
%DISTCHECK calculates ->real<- distance between spheres (gap+ or overlap-)
[size11, size12] = size(sph1);
if size11 == 4;
sph1 = sph1.';
end
[size21, size22] = size(sph2);
if size21 == 4;
sph2 = sph2.';
end
distsq = sum( (sph2(1:3)-sph1(1:3)).^2) - (sph1(4) + sph2(4))^2;
if distsq < 0;
distdir = -1;
else
distdir = 1;
end
distance = distdir*sqrt(abs(distsq));
end
example spheres (so you can run it easily):
% example 1:
sph1 = [-0.2357;0.3639;0.3399;0.250];
sph2 = [-0.1611,0.1500,0.0420,0.01500];
% example 2:
sph1 = [-0.2357;0.3639;0.3399;0.250];
sph2 = [-0.1611;0.1500;0.0420;0.01500];
% example 3:
sph1 = [-0.2357,0.3639,0.3399,0.250];
sph2 = [-0.1611;0.1500;0.0420;0.01500];
% example 4:
sph1 = [1,1,1,1]; sph2 = [1,1,2.5,1];
% example 5:
sph1 = [1;1;1;1]; sph2 = [1;1;2.5;1];
% example 6:
sph1 = [1,1,1,1]; sph2 = [1;1;2.5;1];
No matter which of them I use, I get the error. If I copy-paste each step from the function and run it manually, I get a valid result. What am I missing?
Edit: Whole error message and iputs causing them:
>> sph1 = [-0.2357;0.3639;0.3399;0.250];
sph2 = [-0.1611,0.1500,0.0420,0.01500];
>> distcheck(sph1,sph2)
Subscript indices must
either be real positive
integers or logicals.
Edit2: It seems to work when I clean up my workspace but my main script keeps triggering the error message.
Subscript indices must either be real
positive integers or logicals.
Error in testscript (line
116)
distn2 =
distcheck(sphn,cont2);
with
sphn = [-3.065361088086432e-06;5.050735062465379e-05;1.250955109092040e-05;2.500000000000000e-05];
cont2 = [-1.081620971550675e-06 1.500000000000000e-05 1.797244470458586e-05 1.500000000000000e-05];
Edit3: Found the problem, I somehow created a variable called "distcheck" (same as the function name).
  2 Kommentare
Star Strider
Star Strider am 11 Mai 2014
  • What line throws the error?
  • What sizes are sph1 and sph2 when it occurs?
BTW, you can do away with your first two if blocks. If you want sph1 and sph2 to be row vectors, simply state them as:
sph1 = sph1(:)';
sph2 = sph2(:)';
mosch
mosch am 11 Mai 2014
I edited the error message into the main post.
Thanks for the information, I'll use this to clean up a lot of my other code (got the if statements in different functions)!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 11 Mai 2014
Post your exact error message - you forgot to do so, you just snipped out a small chunk of it. I ran your code in the attached file, and it ran fine with no errors whatsoever . Moreover, I don't see anything that should cause such an error.
  2 Kommentare
mosch
mosch am 11 Mai 2014
Bearbeitet: mosch am 11 Mai 2014
Thanks for confirming that the code seems to be ok in and of itself. I edited the error message into the bottom of my original post.
After deleting all workspace variables it seems to work as well O.o My main script, however, produces lots of workspace variables. I wonder which ones could cause the problem...
mosch
mosch am 11 Mai 2014
The problem was in a variable I mistakenly created with the same name as the function. Your answer was helpful in finding that :)
Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by