MATLAB Answers

Simultaneously run a function and record a video

6 views (last 30 days)
Felix on 18 Dec 2012
I am starting to become very disappointed by matlab. After a long search I found at least a workaround to run two functions simultaneously using parallel processing.
It basically looks like this:
parfor i = 1:2
if i==1
However, I get the error message: Error using parallel_function (line 589) Undefined function 'start' for input arguments of type 'double'.
Apparently, the functions you can use inside a parfor loop are limited, and video recording is not possible?? Is there any workaround? I need to record a video while performing manipulations (output_function_x produces some hardware outputs) that have to be synchronized to the video.
But then how do matlab programs like "scanimage" work that control whole microscopes, apparently it must be possible to have video recording and create motor outputs at the same time...


Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 18 Dec 2012
As a first approximation: objects and handle graphics created in one session cannot be passed to another session. (There might be mechanisms that I have not noticed yet.)
MATLAB sessions have their own address space, and there is no shared object memory between them, so when the new session gets created the object variable just becomes a reference to something that does not exist in the new address space.
The sharing mechanisms are similar to what would happen in a message-passing system: MATLAB can pass in a "message" which is a data array, and it can receive back a "message" that is the results, but what would be a valid pointer in the original session is just a numeric value in the second.
Only one session has access to graphics: MATLAB graphics are all handled by a single JAVA thread that does not have access to the memory of the other sessions.
It is possible that you might be able to get further using a different kind of parallelism, but I am not certain about that.


Felix on 19 Dec 2012
Would it be possible to make the video object run in the normal main session where all the variables are available, and only run the other independent function in the other session? I don't understand why suddenly both functions have to be passed to new sessions where all these restrictions apply...
I tried it with matlabpool open 1, but I still get the error message...
Walter Roberson
Walter Roberson on 19 Dec 2012
Unfortunately I have never had PCT to test with. Which is one reason I have never investigated the more obscure behaviors :(
I suggest a test: try starting a timer that does something like disp() a string at regular intervals, and then parfor something that keeps the session busy for a bit, and see if the values are displayed. Or a variation on this: have the timer append "now" to an array at nominally regular intervals, and parfor() something, and then when the parfor finishes, stop the timer and look at the timestamps. This test will allow you to determine whether the timer is active at regular intervals while the parfor() happens, or if instead the timer callbacks are queued until right after the parfor(), or if instead the timer callbacks just don't happen during the parfor().

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!