Error: Attempt to reference field of non-structure array
1 view (last 30 days)
Show older comments
The code:
close all;clear all;clc;
a=arduino('COM14');
a.pinMode(2,'OUTPUT');
a.pinMode(3,'OUTPUT');
a.pinMode(4,'OUTPUT');
a.pinMode(5,'OUTPUT');
a.pinMode(9,'OUTPUT');
a.pinMode(10,'OUTPUT');
vid=videoinput('winvideo',1,'YUY2_160X120');
set(vid,'TriggerRepeat',Inf);
vid.returnedcolorspace='rgb';
vid.FrameGrabInterval=2;
start(vid) %start the video aquisition here
while(vid.FramesAcquired<=200)
data1=getdata(vid,1);
C=imrotate(data1,90);
data=imcomplement(C);
% Now to track yellow objects in real time
% we have to subtract the yellow component
% from the grayscale image to extract the yellow components in the image.
diff_im = imsubtract(data(:,:,3), rgb2gray(data));
diff_im=medfilt2(diff_im,[1 2]); % Use a median filter to filter out noise
diff_im=im2bw(diff_im,0.26); % Convert the resulting grayscale image into a binary image.
diff_im=bwareaopen(diff_im,50); % Remove all those pixels less than 50px
bw=bwlabel(diff_im,8); % Label all the connected components in the image.
% This is where the image blob analysis occur.
% Get a set of properties for each labeled region.
stats=regionprops(bw,'BoundingBox','Centroid');
% Display the image
imshow(C)
hold on
% This is a loop to bound the yellow objects in a rectangular box and
% along with its center coordinate
for object=1:length(stats)
bb=stats(object).BoundingBox;
bc=stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','Y','LineWidth',2);
plot(bc(1),bc(2),'-m+');
a=text(bc(1)+15,bc(2), strcat('X:', num2str(round(bc(1))), 'Y:', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
X=num2str(round(bc(1)));
Y=num2str(round(bc(2)));
if (X>61);
a.digitalWrite(10, 1);
a.digitalWrite(4, 1);
a.digitalWrite(5, 0);
end
if (X<59);
a.digitalWrite(10, 1);
a.digitalWrite(4, 0);
a.digitalWrite(5, 1);
end
if (X<=61);
a.digitalWrite(9, 0);
a.digitalWrite(10, 0);
end
if (X>=59);
a.digitalWrite(9, 0);
a.digitalWrite(10, 0);
end
end
hold off
end
stop(vid); % Video acquisition is halted.
flushdata(vid); % Removes all the processed image strored in RAM.
clear all
The output:
Attempt to reference field of non-structure array
Error in filename (line 51)
a.digitalWrite(10, 1);
What I am trying to achieve here is to make the arduino gives the output when "if" conditions are met.
0 Comments
Accepted Answer
the cyclist
on 5 Mar 2013
It looks like you redefined the variable a in this line
a=text(bc(1)+15,bc(2), strcat('X:', num2str(round(bc(1))), 'Y:', num2str(round(bc(2)))));
so that it is no longer the object coming out of the call to Arduino. Now it is a variable, and in the line that gives the error, you are treating that variable like a structure (which it is not).
More Answers (0)
See Also
Categories
Find more on Image Acquisition Support Packages for Hardware Adaptors (Generic Video Interface) 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!