How to run two codes simultaneously ?

Hello,
How can I run two codes run stimultaneously for ? I want two of them to start exactly on the same time. Is there any way to execute them by starting at the same time.
% invoke function is for the signal I want to play. It triggers another software to start.
invoke(h, 'MeasureNoSave');
% when invoke function begins, the set function below should also begin simultaneously. However, they run after 'invoke' function
% that is, they are asynchronous :(
set(handles.pushbutton1,'String','Playing!');

 Accepted Answer

No, none of the ways of starting multiple functions are able to start the functions at exactly the same time.
In order to start functions at exactly the same time, you need to have two dedicated computers, each with real-time operating system, both of which have been configured to pause until a hardware signal is received. Then the third system that wants to lauch the programs at the same time needs to trigger a single hardware signal that is shared by both of the real-time computers, and the two real-time computers need to be exactly the same electrical distance apart from the source of the hardware signal in order to trigger the two at the same time.
Getting two computers to be the same physical distance apart to within half a hardware instruction cycle is not going to happen (it requires nanometer precision in the distances), but you might be able to adjust them to the same electrical distance using something like inductors.
Getting the two code paths started at exactly the same time (to a specific precision relative to hardware cycle time) might be do-able if difficult, but keeping the two running at the same rate is really hard.

2 Comments

Thank you. That was the answer I was expecting :( I have read some cool stuff called parallel computing in Matlab. However, I am not sure if this can be suitable for my question. There are some examples to run two function simultaneously using parfor function. Do you think that parfor function can be used to execute two different entries simulatenously under gui handles?
Do you think that parfor function can be used to execute two different entries simulatenously under gui handles?
No. Parallel processes do not have access to the graphics handles associated with the client process. Parallel processes run in a different process (except for some very new work to be able to run some of them in different threads in the same process, but with the same sharing restrictions.)
If you are wanting to take measurements synchronized with playing audio, then I recommend that you look at http://psychtoolbox.org/docs/PsychPortAudio . Psychtoolbox is specifically designed for Psychophysical experiments involving audio and video and reaction measurement.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!