MATLAB: When using the MATLAB Engine 7.2 (R2006a) and running the PREVIEW function, why does GETSNAPSHOT return an empty matrix

Image Acquisition Toolbox

When using Image Acquisition Toolbox with the MATLAB Engine, if I use GETSNAPSHOT function while the PREVIEW function is running, I get an empty array. This behavior does not occur when working directly in MATLAB.
The followng steps reproduce this behavior.
1. Download the attached file 'example.c'.
2. Type the following command to compile the file
optsfile = [matlabroot '\bin\win32\mexopts\lccengmatopts.bat'];
mex('-f', optsfile, 'example.c');
3. Run the generated executable by typing the following
!example
The attached file "example.m" implements the same steps directly in MATLAB, where the issue does not occur.

Best Answer

In the example, either the GETSNAPSHOT or the IMSHOW are being executed in the workspace of the callback function for the PREVIEW. This issue can be fixed by explicitly forcing all the GETSNAPSHOT and IMSHOW commands to run in the base workspace. For example,
engEvalString(ep, "p = getsnapshot(vid);");
would be replaced with:
engEvalString(ep, "evalin('base','p = getsnapshot(vid);'");