I understand that you are trying to plot data from a text file in real-time with a specified interval in MATLAB, you can modify your code to update the plot within a loop. Here’s a streamlined version of your code that achieves this:
clc;
clear;
% Open the file
fid = fopen('name.txt', 'r');
% Initialize variables
x = 0; % Start time
dt = 0.008; % Time increment
interval = 0.004; % Real-time plotting interval
numPoints = 257; % Number of data points
% Prepare the figure
figure;
hold on;
xlabel('Time (ms)');
ylabel('Voltage (mV)');
title('ECG of Normal ECG');
grid on;
% Loop through the data points
for i = 1:numPoints
% Read a single data point
d = textscan(fid, '%f', 1);
a = d{:};
% Update the plot
plot(x, a, 'bo'); % Plot as blue circles
drawnow; % Update the plot immediately
% Pause for the real-time interval
pause(interval);
% Increment time
x = x + dt;
end
% Close the file
fclose(fid);
Key Changes:
- drawnow: This function updates the figure window immediately, allowing for real-time plotting.
- Hold on: Keeps the current plot visible while adding new points.
- Figure Setup: Moved outside the loop for efficiency.
- Pause: Used to control the update interval to simulate real-time plotting.
This script reads data from `name.txt` and plots each point in real-time with a 0.004-second interval. Adjust `numPoints` based on your data file's length if needed.
I hope this helps!