plotting graph

조회 수: 2 (최근 30일)
bikal shrestha
bikal shrestha 2012년 5월 14일
답변: Avni Agrawal 2024년 9월 5일
how to plot the graph by reading the file from "name.txt" in a realtime mode with a interval of 0.004. I was able to plot the data from txt file but can't do in realtime mode... this is the code-- %from here clc %x=0:0.008:2.048 x(1)=0; fid = fopen('name.txt', 'r'); for i=1:257 d=textscan(fid,'%n',1); x=x+0.008 y(i)=x; a(i)=d{:}; end for i=1:257 pause on pause (0.004) m=y(i); n=a(i); plot (m,n) ylabel('voltage(mv)') xlabel('time(ms)') title('ECG of normal ECG') grid end fclose(fid)
%program ended any help will be greatly appreciated... thanx in advance..

답변 (1개)

Avni Agrawal
Avni Agrawal 2024년 9월 5일
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!

카테고리

Help CenterFile Exchange에서 Target Computer Setup에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by