How to execute two MATLAB scripts simultaneously

231 views (last 30 days)
How do I execute two MATLAB scripts simultaneously? The one script is recording an EEG measurement while the other script is generating a visual stimuli on the screen. I hope that it is possible! And if it is, it would be nice with a code example.
The script for recording EEG is:
fs= 256;
%%Setting up the dataloggers
% Set up the communication with the dataloggers
ai = analoginput('guadaq',1);
Serial = ai.DeviceSerial;
% Apply general filtering
set(ai.Channel(1),'BPIndex',45); % bandpassfilter the raw-input
set(ai.Channel(1),'NotchIndex',2); % notchfilter the raw-input
% specify samplefrequency and the total number of samples that should
% be obtained before stopping the datatransfer
% Define the filename for logging the data to harddisk. LoggingMode is set to Disk in order
% to stream the data to the harddisk. Use Disk&Memory to stream the data to harddisk and to
% memory. LogToDiskMode is set to Overwrite to replace an existing file with the same
% name.
while ai.SamplesAcquired< measurement_time*fs
data = getdata(ai);
plot(data), grid on
% Saving the data file
outputfile= 'measurement.mat';
save(outputfile, 'data');
clear ai-----
The script for generating a visual stimuli is:
% ---------------------- Psychophysics Toolbox ---------------------------
% By using the Matlab toolbox - psychophysics - two flickering squares are
% implemented. The program uses the refresh rate of the screen in order to
% generate flicker, by shifting a chosen area between the background color
% (black) and a set color - here white.
% Last edited 7th of February 2011
% ------------------------------------------------------------------------
[wPtr,rect]=Screen('OpenWindow',screenNum); % wPtr - designate the window, rect - screen size
monitorFlipInterval=Screen('GetFlipInterval', wPtr); % Refresh time [s]
frame_rate = 1/monitorFlipInterval; % Frame rate [Hz]
p=8; % Sets the frequency for left hand square
P = frame_rate/(2*p); % Frequency [Hz]
q=4; % Sets the frequency for right hand square
Q = frame_rate/(2*q); % Frequency [Hz]
color_black = [255 255 255]; % Defining black color
color_white = [0 0 0]; % Defining white colot
CenX = rect(3)/2; % Center of width
CenY = rect(4)/2; % Center of height
Dis = 200; % Distance between the two squares
SqS = 300; % Square size
pos1=[CenX-Dis/2-SqS CenY-SqS/2 CenX-Dis/2 CenY+SqS/2]; % Position of left hand square
pos2=[CenX+Dis/2 CenY-SqS/2 CenX+Dis/2+SqS CenY+SqS/2]; % Position of right hand square
black=BlackIndex(wPtr); % Find the color lookup table values (black)
white=WhiteIndex(wPtr); % Find the color lookup table values (white)
Screen('FillRect',wPtr,black); % Fills a rectangle the size of the screen to be black
Screen(wPtr, 'Flip'); % Flip the screen from offscreen to onscreen
while toc<1;
Screen('FillRect',wPtr,black); % Blacks the screen
vbl=Screen(wPtr, 'Flip'); % Collect the time for the first flip with vbl
for i=1:300 % "time of run"
% Generate the flickering squares
if mod(i,p)==0
Screen('FillRect',wPtr,color_black, pos1);
Screen('FillRect',wPtr,color_white, pos1);
if mod(i,q)==0
Screen('FillRect',wPtr,color_black, pos2);
Screen('FillRect',wPtr,color_white, pos2);
Screen(wPtr, 'Flip');
Screen('FillRect',wPtr,black); % Blacks the screen
vbl=Screen(wPtr, 'Flip', vbl+(flipSpd*monitorFlipInterval));
while toc<1;
Can you tell if your code will work, Paulo?
Rasa on 9 Jan 2019
How did you resolve the answer of EEG and Visual stimuli finally ? I also need to do this for my research. If you were able to successfully do it, would you send me the code ?

Sign in to comment.

Accepted Answer

Ian on 27 Feb 2011
I am using matlab to control a neurophysiological data aquisition system (DAQ, plexon omniplex), and am also using the psychophysics toolbox (PTB) to generate stimuli, so basically doing what you need to do. You have the following problem much greater than simply running two scripts:
  1. Synchronisation: how will you know that your EEG timestamp is the same as the PTB's flip timestamp?
  2. Communication: do you need information past between display and DAQ matlab scripts at all?
My solutions are the following: I control the omniplex using my PTB code, it uses a LabJack to send out physical voltage pulses (immediately after Screen('flip') in the PTB code!) to control my electrophysiological data aquisition system. I've tested this by feeding these TTLs into my DAQ, and also recording a signal from a photodiode on the screen that displays output from the PTB to ensure they are synchronised together. Without this, you cannot know whether your stimulus and response really are properly aligned, and this will impact your interpretation of your results.
To see my data coming in online I have to use a different matlab script and I simply open another matlab instance. You can spawn secondary matlab sessions that run your script programatically from your first script (assuming you want to run a script called myScript.m), using:
eval('!matlab -nodesktop -nosplash -r "myScript.m" &')
eval('!osascript -e ''tell application "Terminal"'' -e ''activate'' -e ''do script "matlab -nodesktop -nosplash -maci -r \"myScript.m\""'' -e ''end tell''')
To communicate between these two matlab scripts I use UDP using pnet (it comes with PTB, also available on matlab file exchange) to allow both matlab scripts to talk to each other, pass variables and keep my data visualisation locked to what the stimulus was/is doing. You can for example use UDP packet after the Screen('flip') in PTB so your EEG system knows within a few milliseconds when your display changed if you don't know how to use a photodiode to check this.
Another solution, and one I thought was too limited for my needs but may work for you is the Parallel computing toolbox, see this interesting stack overflow question:

Sign in to comment.

More Answers (4)

Jan on 27 Feb 2011
You cannot run two scripts simultaneously in Matlab. Although you can start different threads in e.g. C, there is no guarantee, that the threads are performed simultaneously, because the OS decides about the order of execution and distribution to the different cores. Either you have to use a real-time OS and e.g. C. Or you can start two Matlab instances to run the two programs. Then I assume you need a synchronization, e.g. a light-diode scanning the screen - this has the advantage, that even the delay between Matlab's figure update and the display on the screen is considered.
Sabrina Wendt
Sabrina Wendt on 28 Feb 2011
Thanks for the illuminating but sad answer. It is not that important that the scripts are synchronized - the most important thing is that the EEG is sampled with a constant sampling frequency and that the stimuli is a constant flicker / constant frequency.

Sign in to comment.

Paulo Silva
Paulo Silva on 26 Feb 2011
I made this code yesterday, it displays the memory used by matlab in real time, it updates the plot every half second, in your case it might work with only a timer but you could add another timer with almost the same parameters except the callback function, for example t would record the EEG and t1 would generate the stimuli, in the callback functions you can just insert the m file name you already have for each job you want to perform.
Without knowing more about your scripts it's not easy to know if this approach would work.
function test
fig=figure;[user,sys] = memory;a=0;
h = plot(ax,NaN, NaN,'LineWidth',5);
set(ax,'YLim',[0 sys.PhysicalMemory.Total/10^6])
ylabel('Memory used by matlab in Megabytes');
t = timer('TimerFcn',@readdata,...
'Period', 0.5,'executionmode','fixedSpacing');start(t)
function readdata(g1,gg1)
if (~ishandle(fig)),stop(t),return,end
set(h,'XData', [get(h, 'XData'), a],...
'YData', [get(h, 'YData'), mem/10^6]);
if numel(xd)>=100
Paulo Silva
Paulo Silva on 27 Feb 2011
I finally found that Jan is right, if I put pause(1) in one of the functions the other will wait, that ain't good, I hope you can do the same like Ian did.
Sorry about the timers, I was sure that would work but it didn't.

Sign in to comment.

Rob Purser
Rob Purser on 28 Feb 2011
The Data Acquisition Toolbox has the exact capability you want built in. There's a demo called "Using Callback Functions to Create Live Displays and Analysis" that is part of the product that teaches you how to do this.
Also, check out the "daqscope" demo that is part of the toolbox for a real world example of plotting the data as it arrives. It's more complex, but shows many of the techniques in a full application.
-Rob Purser
Manager of Test and Measurement
Walter Roberson
Walter Roberson on 16 Jun 2011
Hmmm .. _possibly_ "Acquiring Data and Generating Signals at the Same Time" ??

Sign in to comment.

hurrch on 19 Oct 2019
Good day
sorry i have to sneak into this conversation but i really need your expertise Sire.
I am a final year student of an institution in Africa and I have issues with my chosen project cause I do not have prior knowledge of coding or programming.
Please, I really need help with this project of mine as it will be due in 1 weeks’ time (1/11/2019), I don’t mind being assigned to someone good with the matlab DAQ toolbox for an extra fee. This is the crux of my project.
I am to write a code that will read data from two NI 9237 modules (6 channels in total). the modules are to read data from a force balance (strain due to force). The final result should be different plots for different force values against time. I will explain with a diagram and a flow chart, please do correct the flow chart if it is wrong. Some where in the code, there should be a scaling coefficient that will convert strain to force as needed for the equations. The code should plot the data in real time and also at the end of the acquisition. And the plots are individual force values against time. E.g N against time, M against time etc.
This is what I just did playing around with the code but I’m technically stuck afterwards.
s = daq.createSession ('ni');
ch1 = s.addAnalogInputChannel('Dev1', 'ai0', 'Bridge');
ch2 = s.addAnalogInputChannel('Dev1', 'ai1', 'Bridge');
ch3 = s.addAnalogInputChannel('Dev1', 'ai2', 'Bridge');
ch4 = s.addAnalogInputChannel('Dev2', 'ai0', 'Bridge');
ch5 = s.addAnalogInputChannel('Dev2', 'ai1', 'Bridge');
ch6 = s.addAnalogInputChannel('Dev2', 'ai2', 'Bridge');
ch1.BridgeMode = 'Full';
ch1.ExcitationVoltage = 10;
ch1.NominalBridgeResistance = 350;
ch2.BridgeMode = 'Full';
ch2.ExcitationVoltage = 10;
ch2.NominalBridgeResistance = 350;
ch3.BridgeMode = 'Full';
ch3.ExcitationVoltage = 10;
ch3.NominalBridgeResistance = 350;
ch4.BridgeMode = 'Full';
ch4.ExcitationVoltage = 10;
ch4.NominalBridgeResistance = 350;
ch5.BridgeMode = 'Full';
ch5.ExcitationVoltage = 10;
ch5.NominalBridgeResistance = 350;
ch6.BridgeMode = 'Full';
ch6.ExcitationVoltage = 10;
ch6.NominalBridgeResistance = 350;
attached below is the document to help understand what exactly i am doing and how far i have gone. it also has the flow chart, diagrams and equations.
i really do anticipate your reply.
thank you so much


Find more on Counter and Timer Input and Output 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!