A function within my for-loop is only executing one time, when I need it to continually output updated values.
1 view (last 30 days)
Scott Kaiser on 18 Jul 2020
Hello! I have been having issues with a function that I am calling within a for-loop. The function returns values upon the 1st iteration of the for-loop but thereafter is actually skipped all together. Note that the for-loop still runs all the other code within it, but the function inside it is skipped after iteration 1. I have posted my main function below. The troublesome function ( called ServoAngles() ) within the for-loop is in another '.m' file. I will also post that code as well, in case something is amiss there as well. Thank you for any suggestions and suggestions.
What I have done so far to resolve this issue:
1. Used breakpoints to ensure correct values are being passes into ServoAngles() as arguments.
2. Stepped through the loop. The first iteration will go into ServoAngles() and return values; whereas the 2nd-last iteration will not even go into ServoAngles().
3. I watched a quick debugging video suggested in another video by ImageAnalyst but still no success.
%Here I initialize variables
I = step(cam);
h = imagesc(I);
set(h, 'EraseMode', 'none');
% Ill load an image, store it as ImgSquaresOnBarrier for next line to utilize.
%Take a picture and filter out all colors except hunter orange. Find centroids. Send info to ServoAngles()
img = step(cam);
BW= bwareafilt(BW, 1);
s = regionprops(BW, 'centroid');
%This is the line that only returns values one time and is skipped after iteration one.
%%My function ServoAngles() is below, which is in a seperate .m file.
%Coords of new origin
%Centroid coords in shifted system with origin at center of the image.
%Translate new coords to inches
%Calculate Angle(in degrees) that servos need to point to target based on distToBarrier
Walter Roberson on 19 Jul 2020
The catch was being invoked. The reason is
That replaces the function size() with a variable named size that was not being expected.
We recommend against naming a variable size or sum or length as it is very common to want to use the functions of the same name after having created a variable with those names. And it confuses readers.