MATLAB: Running two matlab scripts simultaneously

running two scripts

I want to run two Matlab scripts simultaneously. First Matlab script is the main program which is continuously looking for a particular situation to occur. Once that situation occurs, it should import data from second script to make a final decision. In the first script, I am using National Instrument’s DAQ and Data Acquisition Toolbox to collect data from a sensor. I am using session-based interface and a listener to collect the data in the background. In the second script, I am using Matlab’s serial port object to collect data from another sensor.
I would really appreciate any help in the matter.
*First Matlab Script:*
s = daq.createSession('ni');
ch1 = s.addAnalogInputChannel('Dev1',1,'Voltage');
ch1.InputType = 'SingleEnded';
set(ch1,'Range', [-5 5]);
s.Rate = 125000;
s.NotifyWhenDataAvailableExceeds = 16384;
s.DurationInSeconds = 10;
lh = s.addlistener('DataAvailable',@(src,event) FFT_Of_Window(event.Data,...
src.Rate,src.NotifyWhenDataAvailableExceeds));
s.startBackground();
s.wait();
delete(lh)
Function FFT_Of_Window
function FFT_Of_Window(Data,Fs,window_Size)
data_Length = length(Data);
fft_Full =fftshift(abs(fft(Data)))/(data_Length/2);
fft_RHS = fft_Full((data_Length/2)+1:data_Length);
KNN_Classifier(fft_RHS)
end
Function KNN_Classifier
function KNN_Classifier(fft_RHS)
output = predict(mdl,fft_RHS);
end
Second Matlab Script:
ser = serial('COM17');
ser.BaudRate = 115200;
fopen(ser);
flushinput(ser);
flushoutput(ser);
while true
data = fread(ser,1);
end

Best Answer

To run two MATLAB scripts simultaneously, you need to have the Parallel Computing Toolbox. In order for the two to be able to communicate, you will need to use spmd() with labsend() and labreceive()
I do not think you actually need to run your two scripts simultaneously: I think you could accomplish what you want with callbacks. I am, however, too tired at the moment to analyze it further.