Plotting real time data using MATLAB & Arduino

Hi!

I'm working on a project that involves displaying the real time data acquired from a variable resistor on the monitor. I am using MATLAB along with Arduino for this purpose.

I am unable to display the real time data.

This is the code I am using for Arduino:

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  
     Serial.println(analogRead(A0));

}

This is the code I am using for MATLAB:

clc;
clear;
arduino = serial('COM41', 'BaudRate', 9600);
fopen(arduino);
x=linspace(1,100);
for i=1:length(x)
    y(i)=fscanf(arduino, '%d');
end
fclose(arduino);
title('Breathing Pattern')
plot(x,y)
xlabel('Time in seconds');
ylabel('Digital Value');

I have attached a picture of the output I get. The graph doesn't depict the variation in the potentiometer. When I ran the program, the potentiometer was kept at a constant value, yet the graph shows varying values. The value of the potentiometer displayed in the graph doesn't correspond to the actual value of the potentiometer. I ran the program using Arduino and used the Serial monitor to verify the value.

output graph.jpg

Hello,
I have a similar problem. I am using MPU 6050 to get accelero and gyro data. I am able to see the change in the arduino serial monitor as i am moving with sensor but not able to see change in the Matlab plot.

Here is my Matlab code:

if exist('s','var')
fclose(s)
instrreset
clear
close all
clc
end

s = serial('COM7');
set(s,'BaudRate',38400);
set(s,'DataBits',8);
set(s,'StopBits',1);
fopen(s);

time=0:1/116:10;
time=time';

for i=1:500
data(:,i)=fscanf(s,'%f %d %d %d %d %d');
if i==1
data(:,1)=[0;0;0;0;0;0];
else
end
figure(1)
subplot(3,1,1)
hold on
plot(time(i),data(1,i),'*b')
subplot(3,1,2)
hold on
plot(time(i), data(2,i),'*r')
subplot(3,1,3)
hold on
plot(time(i), data(3,i),'*g')
figure(2)
subplot(3,1,1)
hold on
plot(time(i), 0.0076.*data(4,i),'*b')
subplot(3,1,2)
hold on
plot(time(i), 0.0076.*data(5,i),'*r')
subplot(3,1,3)
hold on
plot(time(i), 0.0076.*data(6,i),'*g')
drawnow
end

Can anyone help me to understand what could be the problem in this?