Real Time Functionality of Matlab -- I need Matlab to find an image file and open it once the image file is created.

2 views (last 30 days)
I am doing a project which automates the capture of a webcam. I need matlab to check the image file repetitively until it finds it that is, matlab will able to find it only when the camera captures an image.
Here is my code for iteratively opening a file:
uint8 img;
img = 0;
while img == 0
img = imread('F:\pictures\irisgray1.png');
end
This code however has an error on this line [img = imread('F:\pictures\irisgray1.png');] because irisgray1.png is not yet created on the directory because the camera does not captures anything yet. I need to have a code that will check the file until it is created.

Accepted Answer

Walter Roberson
Walter Roberson on 6 May 2011
Your line
uint8 img;
is equivalent to coding
uint8('img');
which is to say, convert the string 'img' to uint8 and then throw the result away.
To test whether a file exists, use
exists('F:\pictures\irisgray1.png','file')
Be warned though that this could use up cpu time like crazy and prevent other things from happening on the system.
Also be warned that this could potentially go true as soon as the file is created, which is probably not what you want. What you want to know is when the camera software has finished writing to the file. There really isn't any way to know that, unless the camera software takes the precaution of creating the file under a different name and renaming it to the known name after it has finished writing to it. With Windows, you also might get lucky and have it refuse to open the file if it is being written to by another program, but I wouldn't want to count on that.
Filesystems are usually designed for cooperative multitasking (i.e., all programs interested in a file use some common protocol to know if access is okay). The alternative, mandatory locking, has well known problems with "deadlocks", and supporting mandatory locking properly can require that the operating system periodically takes a complete "snapshot" of a program, and when a deadlock occurs, "unwinds" the program states to the point where there is no more deadlock. This can get messy, as the state of all other processes that are being communicated with might also have to be unwound. Messy, messy, messy. So much so that it is seldom implemented, as it is often much less hassle to go back and write all the other programs to use cooperative multitasking.
  3 Comments
Paulo
Paulo on 6 May 2011
i got this error:
??? Undefined function or variable 'img'.
Error in ==> open at 2
while img == 0
with this code:
uint('img');
while img == 0
img = exist('F:\pictures\irisgray1.png','file');
end

Sign in to comment.

More Answers (2)

Matt Fig
Matt Fig on 6 May 2011
You could set a timer callback to do this. Start by creating a list of the images in the file. This list will be updated and stored each time the timer callback fires. When the callback starts, make a call to DIR to get a list of *png files, then compare it to the stored list. If any name is in the directory but not in the stored list, send the name to IMREAD.
You don't show what you are doing with the image once it is read, this might affect what to do next....

Eric Keller
Eric Keller on 6 May 2011
unless matlab has changed recently, it doesn't actually try to see if the file exists before it tells you it doesn't exist. It uses some cached directory listing. There is a command called "rehash" that will make it check. Looking at the help for rehash, it doesn't look like anything has changed.
If you really have problems with reading a file before the other program has finished writing it, you could write it to a different name and then change the name as the last thing. Much less likely to have a conflict. I have had to do this with gnuplot, but never with matlab.

Categories

Find more on Startup and Shutdown 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!