matlab code overload (busy) and not doing what it's supposed to do

2 views (last 30 days)
Hi
I have written an image acquisision and proccessing code which is invoked every time a timer object stops running, or not responding.
the whole project consists 3 fuctions: 1)timer definition 2)image acquisition 3)image processing
when Im debugging the program, everything is working great. when Im making an executable of my program usin the deployment tool, it starts, but closes itself, or not doing anything. if it doesnt close, it just overloads my cpu (to 100%).
The algorithm's run time is infinite, because I am running the timer at a 'fixed spacing' running mode.
the codes are:
1)
function []= timer_me()
clear
clc
close all
%a=imread('tire.tif');
%imshow(a);
global oldfile;
path = 'C:\MATLAB PROJECTS\dirtest';
test=dir(path);
oldfile=test(size(test)).name;
out = timerfind('Name', 'ReadImageTimer');
if(size(out)==0)
t = timer('Name','ReadImageTimer',...
'ExecutionMode', 'fixedSpacing',...
'TimerFcn',@readimage,...
'Period',0.5 );
start(t);
end
while(1);
end-----------------------------
2)
function [ ] = readimage(arg1, arg2, arg3)
path = 'C:\MATLAB PROJECTS\dirtest';
test=dir(path);
filename=test(size(test)).name;
global oldfile
if(~strcmp(oldfile, filename))%IS THE OLDEST FILE IN THE
%DIRECTORY NEW?
i=imread([path '\\' filename]);
imshow(i);
[hit]=coloralg(i);
oldfile=filename;
hit
subplot(2,1,1), imshow(i);
if(hit == 1)
subplot(2,1,1), imshow(i);
xlabel('hit!')
ylabel('hit!')
else
xlabel('miss!')
ylabel('miss!')
end
else
a='same'
end
% start(timer)
end
-------------------------
3)
function [hit] = coloralg(x)
%type=3; %in the actual func will get 1/2/3 according to R/G/B
%[x]=imread('black_red.jpg');
%x=data;
subplot(2,1,1), imshow(x), ylabel('before');
xred=x(:,:,1);
%xgreen=x(:,:,2);
%xblue=x(:,:,3);
[numr,numc,null]=size(x);
quant=90;
for r=1:numr
for c=1:numc
if x(r,c,1)>110
if x(r,c,2)<230
if x(r,c,3)<230
x(r,c,:)=255;
else
x(r,c,:)=0;
end
else
x(r,c,:)=0;
end
else
x(r,c,:)=0;
end
end
end
%xafterred=x(:,:,1);
%xaftergreen=x(:,:,2);
%xafterblue=x(:,:,3);
subplot(2,1,2), imshow(x), ylabel('after ');
middlex=round(size(x)/2);
middlexr=[middlex(1),middlex(2)];
measurepointr = [middlexr(1), middlexr(2) , 1];
wtf=x(measurepointr(1),measurepointr(2),1)
a=imread('tire.tif');
imshow(a);
if(wtf == 255)
hit = 1;
else
hit = 0;
end
------------
Can somebody help me please?
Matar Maoz

Accepted Answer

Jan
Jan on 30 Jun 2011
Avoid using "path" as name of a variable. This possibly shadows the important function PATH.
Using == with vectors in an IF condition is not clean:
out = timerfind('Name', 'ReadImageTimer');
if(size(out)==0)
It works here, but prefer:
if isempty(out)
Using CLEAR directly after the function header is meaningless. So better omit it.
I do not think that this does what you expect:
test = dir(path);
filename = test(size(test)).name;
"size(test)" replies a vector. I assume you want "length(test)". But even then, the assumption, that the last file is the oldest is fragile. The reply of the DIR command is sorted alphabetically, but even this is not documented. Better use the datenum's replied by DIR also to find the oldest file.
This is a cruel method to waste energy:
while(1)
end
Because this loop is called infinitely, the timer's callback has no chance to be invoked. At least insert a DRAWNOW, but PAUSE(0.1) would be more friendly. I'd avoid the unnecessary increase of complexity of using a TIMER and use the loop in the main program directly:
while 1
pause(0.5);
readimage;
end
BTW.: The 3 inputs of readimag() are not used anywhere. If you use a modern Matlab version, some warning should appear.
Finally I cannot believe that this program "runs great" when debugging.
  4 Comments
Paulo Silva
Paulo Silva on 1 Jul 2011
like Jan said you can use the loop and pause instead of the timer, each way has some different problems.

Sign in to comment.

More Answers (0)

Categories

Find more on App Building 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!