If you do not know the number of frames a priori, do not try to expand mxArray in C. This is not convenient. You were already close to the beginning. All your problems can be solved with:
engEvalString(engine, "your command here")
More details here .
The simplest approach is something like:
engPutVariable(engine, "mat", mat); engEvalString("frames{length(frames)+1} = mat;");
Not doing it this way is an illustration and will be very slow. It is much better to preset, say, 1000 frames, and then expand it with another 1000 (or a more suitable number), when necessary. Even better, don't use arrays of cells that are slow. Instead, you can use a 3D array, for example:
frames = zeros(13,13,1000); frames(:,:,i) = mat; i = i + 1;
Again, pre-select in blocks. You get the idea. If you really need to be fast, you can build 3D arrays in C and send them to MATLAB when they are full.
source share