error:Undefined function or variable 'values'.
2 次查看(过去 30 天)
显示 更早的评论
clc;
clear all;
CAMERA = videoinput('pointgrey', '1');%camera initialization
start(CAMERA);
a = serial('COM3','BaudRate',9600); %arduino initialization
fopen(a);
pause(1);
for a = 1:8
automate(CAMERA)
pause(8)
end
plot(values); %plotting values just for our visual understanding
[m,index] = max(values)
fprintf(a,'%d',index) %"here is where we send the position of image to arduino"
fclose(a)
hold on
plot(index,m,'*')
hold off
title("bodekke with spot images")
stop(CAMERA); %stop camera acquisition
function[addition] = bodekke(imagename) %bodekke function mathematics behind the autofocus detection
im = double(imread(imagename))
bd = [-1 0 1]
cbd = conv2(im,bd)
squaredcbd = cbd.^2
addition = sum(squaredcbd(:))
end
function automate(CAMERA)
for i = 1:30 %to capture images
filename = sprintf('testtry%02d.jpg',i);
img = im2double(getsnapshot(CAMERA)); %captures images
img = rgb2gray(img);
imwrite(img,filename)
end
D = 'C:\Users\PRL\Desktop\just'; %location where image is stored
S = dir(fullfile(D,'*.jpg'));
for k = 1:numel(S) %applies bodekke to all images
F = fullfile(D,S(k).name);
queen(k) = bodekke(F); %bodekke function call
end
end
26 个评论
Adam
2020-3-3
You clearly aren't doing. Given that you have
clear all
at the top, then only these lines will have any impact before the plot instruction:
CAMERA = videoinput('pointgrey', '1');%camera initialization
start(CAMERA);
a = serial('COM3','BaudRate',9600); %arduino initialization
fopen(a);
pause(1);
for a = 1:8
automate(CAMERA)
pause(8)
end
None of these lines assigns anything to 'values'
barath V
2020-3-3
clc;
clear all;
CAMERA = videoinput('pointgrey', '1');%camera initialization
start(CAMERA);
ard = serial('COM3','BaudRate',9600); %arduino initialization
fopen(ard);
pause(1);
for b = 1:8
automate(CAMERA)
pause(8)
end
fclose(ard)
stop(CAMERA); %stop camera acquisition
function[addition] = bodekke(imagename) %bodekke function mathematics behind the autofocus detection
im = double(imread(imagename))
bd = [-1 0 1]
cbd = conv2(im,bd)
squaredcbd = cbd.^2
addition = sum(squaredcbd(:))
end
function automate(CAMERA)
for i = 1:30 %to capture images
filename = sprintf('testtry%02d.jpg',i);
img = im2double(getsnapshot(CAMERA)); %captures images
img = rgb2gray(img);
imwrite(img,filename)
end
D = 'C:\Users\PRL\Desktop\just'; %location where image is stored
S = dir(fullfile(D,'*.jpg'));
for k = 1:numel(S) %applies bodekke to all images
F = fullfile(D,S(k).name);
addition(k) = bodekke(F); %bodekke function call
end
plot(addition); %plotting values just for our visual understanding
[m,index] = max(addition)
hold on
plot(index,m,'*')
hold off
title("bodekke with spot images")
fprintf(ard,'%d',index) %"here is where we send the position of image to arduino"
end
barath V
2020-3-3
here i have changed variable name to addition and i have assigned the function call to the variable,here i get a error
Undefined function or variable 'ard'.
fprintf(ard,'%d',index) %"here is where we send the position of image to arduino"
Adam
2020-3-3
编辑:Adam
2020-3-3
You need to get to understand how different workspaces work in Matlab (and many languages, for that matter)
A function has its own sealed workspace. The only variables it sees are those you pass in (unless it is a nested function), the only variables the outside world will see are the ones it returns as arguments. Once you understand the scope of workspaces and error like this is 100% self-explanatory from the message it gives.
You define ard in your code at the top, but you do not pass it to the automate function so this function does not know of a variable called ard, hence the error. Pass it in as an argument if you wish to use it in the function.
barath V
2020-3-5
hi, i defined the object ard inside the function itself,but the close function for ard i have defined outside.
the motors are rotating,the actual execution of the code happens only once,and then i get this com port error and fopen() error.
clc;
clear all;
CAMERA = videoinput('pointgrey', '1');%camera initialization
start(CAMERA);
for b = 1:8
automate(CAMERA)
pause(8)
end
fclose(ard)
stop(CAMERA); %stop camera acquisition
function[addition] = bodekke(imagename) %bodekke function mathematics behind the autofocus detection
im = double(imread(imagename))
bd = [-1 0 1]
cbd = conv2(im,bd)
squaredcbd = cbd.^2
addition = sum(squaredcbd(:))
end
function automate(CAMERA)
ard = serial('COM3','BaudRate',9600); %arduino initialization
fopen(ard);
pause(1);
for i = 1:30 %to capture images
filename = sprintf('testtry%02d.jpg',i);
img = im2double(getsnapshot(CAMERA)); %captures images
img = rgb2gray(img);
imwrite(img,filename)
end
D = 'C:\Users\PRL\Desktop\just'; %location where image is stored
S = dir(fullfile(D,'*.jpg'));
for k = 1:numel(S) %applies bodekke to all images
F = fullfile(D,S(k).name);
addition(k) = bodekke(F); %bodekke function call
end
plot(addition); %plotting values just for our visual understanding
[m,index] = max(addition)
hold on
plot(index,m,'*')
hold off
title("bodekke with spot images")
fprintf(ard,'%d',index) %"here is where we send the position of image to arduino"
end
Open failed: Port: COM3 is not available. Available ports: COM1.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.
fopen(ard);
KALYAN ACHARJYA
2020-3-5
This error is completely different from previous one. May be: please look on the avalible port number in arduino interface.
barath V
2020-3-5
i checked its COM3 only,this error occurs because i open the ardunio object ard,again while it is already open in the first itersation of function call.
i want to know how to use this line inside the function,without creating an object for ard inside the function.
fprintf(ard,'%d',index)
barath V
2020-3-5
yes it works, i tried that.but i cant see the plot and the fprintf() working during everey function call.
fprintf() works only during the first time and plot works only at the last.
Walter Roberson
2020-3-5
You are not calling drawnow() or pause(), so you will only see the final graphics result. drawnow() or pause() tell the graphics system that the changes to the graphics objects can be sent to the display.
You are not sending a line terminator with your fprintf()
barath V
2020-3-6
i used drawnow,but that doesnt solve the issue.
clc;
clear all;
CAMERA = videoinput('pointgrey', '1');%camera initialization
start(CAMERA);
ard = serial('COM3','BaudRate',9600); %arduino initialization
fopen(ard);
pause(1)
for b = 1:8
automate(CAMERA,ard)
pause(8)
end
fclose(ard)
stop(CAMERA); %stop camera acquisition
function[addition] = bodekke(imagename) %bodekke function mathematics behind the autofocus detection
im = double(imread(imagename))
bd = [-1 0 1]
cbd = conv2(im,bd)
squaredcbd = cbd.^2
addition = sum(squaredcbd(:))
end
function automate(CAMERA,ard)
for i = 1:30 %to capture images
filename = sprintf('testtry%02d.jpg',i);
img = im2double(getsnapshot(CAMERA)); %captures images
img = rgb2gray(img);
imwrite(img,filename)
end
D = 'C:\Users\PRL\Desktop\just'; %location where image is stored
S = dir(fullfile(D,'*.jpg'));
for k = 1:numel(S) %applies bodekke to all images
F = fullfile(D,S(k).name);
addition(k) = bodekke(F); %bodekke function call
end
[m,index] = max(addition)
fprintf(ard,'%d',index) %"here is where we send the position of image to arduino"
plot(addition); %plotting values just for our visual understanding
hold on
plot(index,m,'*')
hold off
title("bodekke with spot images")
drawnow
end
Walter Roberson
2020-3-6
I am not clear as to what you are saying? Are you saying that plot(addition) is not producing a visual result, but the marking of the maximum is working?
You are still not sending a line termination to the arduino '%d\n'
barath V
2020-3-6
Yes,the plot addition is not producing a visual result during each and every function call. I can see the plot only during last function call.
I changed the codes now,I added line termination to Arduino.
Walter Roberson
2020-3-8
You imwrite using the filename pattern testtry%02d.jpg but it is not clear that your current directory is the same as the directory that you look inside, 'C:\Users\PRL\Desktop\just' . It would be better for you to move the assignment to D to before the for i loop, and that you use
imwrite(img, fullfile(D,filename))
barath V
2020-3-9
i open the directory path in matlab and keep so the image gets copied there,anyway ill change it.
but how to solve the problem of plots i mentioned earlier?
barath V
2020-3-9
iam always getting the comport error also,i can only run the codes once,after it gets finished i again run the codes,but this time i get comport error,so i have to close and restart matlab again,then the code runs.
i ahve to do it each and everey time.
how to solve this error.
Walter Roberson
2020-3-9
Use instrfind() to locate the com port object, and delete() the object. And change your code to delete(a) after you fclose(a)
Walter Roberson
2020-3-9
For the purpose of debugging, after
title("bodekke with spot images")
add
fprintf('%d of %d elements in addition are finite', nnz(isfinite(addition)), numel(addition));
barath V
2020-3-20
i want to thank mr.walter roberson and matlab people for continuously supporting my projects,thanks mr.walter
回答(1 个)
barath V
2020-3-9
hi deleting the arduino object solved the pblm,thanlks.
how can i capture a video?
3 个评论
Walter Roberson
2020-3-20
Your automate function is already capturing the video and writing it to file.
barath V
2020-3-21
Yes,i capture imagea and analyse it using bodekke,later these images are overwritten. Iam trying to capture a video after i send a value to Arduino,that will be permanent and wont be overwritten or analysed.
Walter Roberson
2020-3-21
Use different folders instead of always using
D = 'C:\Users\PRL\Desktop\just';
You could use uigetdir to have the user tell you the directory. Or you could look in the folder and see what subfolders already exist and use the next available one.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Support Package for USB Webcams 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)