First, delete the preview(vid)
line, this is probably why the rep. rate you are getting is weird. When you take data you don't need this preview option on, as it takes resources from your cpu.
Then, you may need to set the camera properties on the imaq toolbox to be in triggered mode. For example, for a gentl
camera type this might look something like:
triggerconfig(vid, 'hardware', 'DeviceSpecific', 'DeviceSpecific');
src = getselectedsource(vid);
src.FrameStartTriggerMode = 'On';
src.FrameStartTriggerActivation = 'RisingEdge';
src.FrameStartTriggerDelayAbs = 0;
src.FrameStartTriggerSource = 'Line1';
src.FrameStartTriggerOverlap = 'Off';
Then, with some camera's you can read their trigger out, that is whenever the camera is exposing, it sends a ttl to some output. Matlab way to define it is something like:
src.SyncOut1SyncOutPolarity = 'Normal';
src.SyncOut1SyncOutSource = 'Exposing';
Again, you'll need to play with your camera's options in the imaq tool.
Also, the data processing step that you take afterwards may take some time, so benchmark it to see you can take data and analyze it on the fly without bottlenecks happening.
Last, you can use getdata
instead of getsnapshot
(read the documentation to see their difference) , and in the form: [img, time, metadata] = getdata(vid);
This will give you timestamps for each image taken, so you can see what's happening. Also, instead of clear data
use flushdata(vid)
to keep the vid object from completely filling the memory buffer (though if you only run 100 iterations in a loop, you should be fine).