Clear Filters
Clear Filters

How can I plot graph in Matlab by taking input as a voltage..?

4 views (last 30 days)
Hello Friends, I am new to Matlab but not to programming. I have a system which give output as a change in voltage. Is there any method by which I can plot that change in voltage into graph. That graph must be a real time graph. For example, that one we see in ICU (monitor) that shows the live graph of pulse rate. I would obliged if anyone would send me any study material which could help me.

Answers (1)

Riya
Riya on 5 Feb 2024
Hi Avinash
Plotting real-time data, such as a changing voltage signal, in MATLAB can be done using a combination of MATLAB functions that read the data from your system and plot it continuously. The exact method of reading data will depend on how your system is interfaced with your computer (e.g., through a serial port, USB, GPIB, etc.).
Below is a simple example of how you might set up a real-time plot in MATLAB if you are reading voltage data from a serial port. This example assumes that you have a device connected to a serial port that sends voltage readings as ASCII strings terminated with a newline character.
% Create a serial port object. Replace 'COM3' with the actual COM port
s = serial('COM3', 'BaudRate', 9600, 'Terminator', 'LF');
fopen(s);
% Set up the plot
figure;
h = plot(NaN, NaN); % Initialize the plot with not-a-number values
xlabel('Time');
ylabel('Voltage (V)');
title('Real-time Voltage Plot');
% Set up a counter for the x-axis (time)
counter = 1;
% Start the real-time data acquisition and plotting
while true
% Read the data from the serial port
data = fgetl(s);
% Convert the string data to a number
voltage = str2double(data);
% Update the plot data
x = get(h, 'XData');
y = get(h, 'YData');
x = [x counter];
y = [y voltage];
% Update the plot
set(h, 'XData', x, 'YData', y);
drawnow; % Update the plot immediately
% Increment the counter
counter = counter + 1;
% Optional: break the loop after a certain condition or time
% if counter > 1000
% break;
% end
% Optional: add a pause to control the update rate
% pause(0.1);
end
% Close the serial port when done
fclose(s);
delete(s);
clear s;
Please note that you will need to replace `'COM3'` with the actual COM port that your voltage sensing device is connected to, and set the `'BaudRate'` to match the configuration of your device.
This example uses an infinite loop to continuously read data and update the plot. You can stop the loop by pressing Ctrl+C in the Command Window or by adding a break condition inside the loop if when the input data stream endsed.
Before running this code, make sure you have the necessary permissions to access the COM port and that no other applications are using it.
Remember, this is just a simple example and may need to be adapted to your specific hardware and requirements. Also, for real-time plotting, MATLAB's performance may vary based on your system's capabilities, and the plot may lag if the data rate is too high.
For more information on serial communication in MATLAB and real-time plotting, you can refer to the MATLAB documentation:

Categories

Find more on Dates and Time 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!