필터 지우기
필터 지우기

Execute two lines simultaneously

조회 수: 25 (최근 30일)
Zoe
Zoe 2024년 3월 18일
댓글: Sam Marshalik 2024년 3월 19일
Hi, I am new to Matlab so please be pateint with me.
What I need to do is tell one machine to turn on while simultaneously collecting data of the resulting change to the system during the work of the machine. Imagine I am using Matlab to turn on the stove while also using Matlab to record the rising temperature of the water while the stove is on.
Specifically, I am trying to write a code that controls the settings of a machine that delivers electrical stimulation to a mouse (this is the A-M Systems Model 4100 High Powered Stimulator) and then records the response of the mouse, which is measured from another machine that measures signal similar to an ECG (this data is interpreted from an NI USB device that records straight to Matlab). The issue is that I do not know how to send a command to the machine to work while simulatneously begin recording data. The code as it exists now tells the machine to generate electrical pulses and then waits for the pulses to conclude before executing the command to record the data.
Here is the snippet of the code that I want to run simultaneously:
Send2Stimulator(sp,'1001 set trigger free'); % this generates electrical pulses for 10 seconds
data = read(dq, seconds(10)); %this records the data from the NI device for 10 seconds
I need the data to be recorded during the electrical pulses.
In case it is relevant, here is the function for Send2Stimulator. This is a function directly from the given Matlab code for the stimulator, I did not write it. There is an entire manual of code for this machine, so you will just have to trust me when I say that the above code will generate electrical pulses for 10 seconds (basically there is a variable that is set to 10s but it is done with some difficulty).
function outstr=Send2Stimulator(port,instr)
writeline(port,instr); % write the instruction
%fprintf('Send= %s\t\t',instr); %display the instruction strring
lastwarn(''); % clear warnings
rplyStr=readline(port); % read the reply from the instrument
if(~isempty(lastwarn)) % if a wanrning occurs read the bytes reply
lastwarn('');
if port.NumBytesAvailable>0
rplyStr=char(read(port,port.NumBytesAvailable,'uint8')); % use a byte read instead of string read
rplyStr=erase(rplyStr,char(13)); % remove charriage returns
rplyStr=strrep(rplyStr,newline,'~'); %replace line feeds with ~
rplyStr=strrep(rplyStr,'~~','~');
else
rplyStr='error no data';
end
fprintf(2,'\t \t \t \t ERROR Reply= %s \n',rplyStr);
fprintf('\n');
else
rplyStr=strcat(rplyStr,'*');
rplyStr=erase(rplyStr,char(13)); % remove charriage returns
rplyStr=strrep(rplyStr,newline,'~'); %replace line feeds with ~
rplyStr=strrep(rplyStr,'~~','~');
%fprintf('Reply= %s \n', rplyStr); %display reply
end
outstr=rplyStr;
end
Here is the code used to initialize the NI USB device (again probably not relevant)
dq = daq("ni");
ch = addinput(dq,"Dev1", "ai0","Voltage");
Any help is greatly appreciated! Let me know if you need more information, hopefully I have included everything you need to understand the problem.

답변 (1개)

Pratyush
Pratyush 2024년 3월 19일
Hi Zoe,
To run electrical stimulation and data recording simultaneously in MATLAB, you can use the "parfeval" function from the Parallel Computing Toolbox. This function allows you to execute tasks in the background, enabling both the stimulation of the A-M Systems Model 4100 High Powered Stimulator and the recording of data from an NI USB device without waiting for each task to complete sequentially. The approach involves:
  • If not already done, initialize a parallel pool in MATLAB to enable background execution of tasks.
  • Create separate functions for the stimulation and data recording tasks. The stimulation function wraps the existing "Send2Stimulator" call, while the recording function handles data acquisition from the NI device.
  • Execute the stimulation function in the background using "parfeval", allowing MATLAB to proceed without waiting for it to finish.
  • Start the data recording either in the main thread or as another background task immediately after initiating the stimulation. This ensures both processes run concurrently.
  • You can optionally wait for the background stimulation task to complete if synchronization between tasks is necessary.
This method facilitates simultaneous operation of stimulation and data recording, crucial for experiments requiring real-time data acquisition during stimulation. However, achieving precise timing and synchronization may require additional adjustments, depending on the specific hardware and experimental requirements. Always test thoroughly to ensure the setup meets your needs.
  댓글 수: 1
Sam Marshalik
Sam Marshalik 2024년 3월 19일
You could also leverage backgroundPool for this type of a workflow, if you do not have Parallel Computing Toolbox and want to run only a single task in the background. The only caveat I would keep in mind is that backgroundPool utilizes a threaded worker and not all MATLAB functions work on Threads. If you run into issues with Threads, you can use "Processes" instead (read more here).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by