How to run two different programs in matlab ?
Show older comments
Hello!!
I have to run two different programs in matlab simultaneusly. Parallel Computing Toolbox is not an opcion.
The situation is: i have to show a moving image while analyze data in other program,both are totally independent.
I wish you can help me. Thank you!!
Answers (4)
Daniel Shub
on 11 Oct 2012
2 votes
Assuming that running a bit of program 1 followed by a bit of program 2 qualifies as simultaneously, there are a number of ways of doing it.
Multiprocess: The multiprocess approach requires each program to be run as a process with its own resources. The OS then handles the context switching between the processes/programs. The multiprocess approach tends to be the best approach for two long running independent programs. The launch two instances of MATLAB falls into this category, but the category is much wider than that. I believe parfor and the PCT essentially take a multiprocess approach
Multithread: The multithread approach requires a single program to create a thread for each program. These threads share some resources, but also have some independent resources. The multithread approach tends to be the best approach for two long running dependent programs. While many of the built in MATLAB functions are multithreaded, user space MATLAB is designed to be single threaded. There is no direct way to spawn threads, although you can from a mex of java program.
Single thread: The single thread approach involves combining your two program into one program that does both things simultaneously. The use of callbacks, the event queue, and timers, can make this easier. This approach is best for short running dependent programs.
9 Comments
Inmaculada
on 17 Oct 2012
Inmaculada
on 19 Oct 2012
Azzi Abdelmalek
on 19 Oct 2012
Edited: Azzi Abdelmalek
on 19 Oct 2012
Inmaculada, If you lunch two matlab session, then arrange your windows to see at the same time your two programs runing. I think it's the easiest way to do it.
Jason Ross
on 19 Oct 2012
Edited: Jason Ross
on 19 Oct 2012
PCT enables you to launch multiple MATLAB workers on your machine or on a cluster (amoung other things). It's not set up to provide two desktop MATLABs in sync with each other. It's possible that you could do your EEG measurement in the background using a job and then do your visual part in the foreground MATLAB, but if the EEG measurement requires interaction, you aren't going to be able to provide that.
It would be something like
c=parcluster('local');
j=c.createJob();
t=j.createTask(@eeg_function, 3, {inputs})
j.submit
The job would then start running on your local cluster (in other words, your machine). You could also use the batch() command to submit it, too. Take a look at the help for parcluster, createJob and createTask for the meaning of the arguments -- I completely made up the arguments to createTask and they likely don't match your program. Also, if you prefer the non-object syntax, you can use that too and get the same results. If you have a script that runs your EEG function, you might also find the batch command useful.
Launching two MATLABs would be the approach if you are doing two things that require interaction with the program as it runs.
Daniel Shub
on 19 Oct 2012
It is hard for me to see how these ways do not work for you. I don't see why the multiprocess (i.e., two MATLAB sessions) or single thread (with recoding) would not work for you situation. The multithread approach should also work in a suitable programming language (e.g., Python) or if you are willing to create mex functions. The PCT seems unnecessary and unhelpful for your use. Have you looked into the psychtoolbox?
Inmaculada
on 22 Oct 2012
Daniel Shub
on 22 Oct 2012
As long as you think of it as two functions, there is no way to run it simultaneously in MATLAB. You need either to create one function or offload one function to another process/thread. I believe Pscyhtoolbox offloads the drawing to a new thread.
Inmaculada
on 23 Oct 2012
Walter Roberson
on 23 Oct 2012
I believe that psychtoolbox is built with an implicit programming model of "soft realtime", which is a model used when events do not need to happen at exact intervals but instead have a window of time to occur in, making it acceptable for the CPU to be doing other operations as long as the other operations do not take total control of the CPU for long enough to block the event from being completely processed by the end of its time window. As the time window gets shorter and shorter, it becomes less and less practical to do other things and the model becomes "hard real-time".
MATLAB itself is not suitable for "hard real-time", but some of the Simulink toolboxes can be used to generate real-time code to be downloaded into a system that is running a real-time operating system.
If the nature of your drawing work requires that you hook onto the vertical sync pulse of a CRT and use no more than a millisecond or so to rewrite the hardware graphics buffer memory, then MATLAB is not a suitable tool. If, though, you can get away with double-buffering frames any time during the screen draw, then you have a much greater window of opportunity and "soft" real-time becomes plausible with the aid of the psychtoolbox extensions.
You would not, in such a situation, use explicit creation of two tasks. Instead, you would start the data acquisition in background mode, allowing it to interrupt and write to memory from time to time.
Azzi Abdelmalek
on 10 Oct 2012
1 vote
Lunch Matlab twice,
9 Comments
Inmaculada
on 10 Oct 2012
Daniel Shub
on 10 Oct 2012
I especially like the idea of taking MATLAB to lunch.
Azzi Abdelmalek
on 10 Oct 2012
That was Walter's idea
Inmaculada
on 10 Oct 2012
Azzi Abdelmalek
on 10 Oct 2012
No, you start 2 session of Matlab (you will two matlab command)
run the file1 in matlab session 1
run the file2 in matlab session 2
you will 2 windows of two matlab sessions runing at the same time
Azzi Abdelmalek
on 10 Oct 2012
Edited: Azzi Abdelmalek
on 10 Oct 2012
to synchronize the two files execution to start at 19h30 add
while datenum(datestr(clock,'HH:MM:SS'))<datenum('19:30:00')
end
file1
in session 1
and
while datenum(datestr(clock,'HH:MM:SS'))<datenum('19:30:00')
end
file2
in session2
Inmaculada
on 11 Oct 2012
Azzi Abdelmalek
on 11 Oct 2012
You can't
Sachin Ganjare
on 11 Oct 2012
0 votes
Take a look at link given below:
Hope it helps!!!
ARPIT
on 30 May 2017
0 votes
can you kindly let me know how you finally executed it? I also plan to do something similar
1 Comment
Walter Roberson
on 31 May 2017
If you need to display images at precise times, please look at Psychtoolbox
Categories
Find more on Installation and Operational Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!