Attempting to generate a graph from a procedurally generating Arduino code using MATLAB

Hello,

I am attempting to generate a graph on MATLAB using the data that is generated from my Arduino Code:

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

void loop() {
  float capacitance = random(100, 1000) / 100.0; // Generate random capacitance reading between 0 and 10 pF
  Serial.print(capacitance, 2); // Send capacitance value
  Serial.print(","); // Send delimiter character
  Serial.println("pF"); // Send unit string
  delay(1000); // Wait for next reading
}

I don't know where to go from there since MATLAB is still fairly new to me. This is the code I was using:

port = serialport('COM11', 9600);
configureTerminator(port, 'CR/LF');

data = readline(port);
values  = split(data, ',');
numeric_values = str2double(values);
data_over_time = [];
figure;
ylim([0, 1024]);
xlim([0, length(data_over_time)]);

while true
    data = readline(port);
    values = split(data, ",");
    numeric_values = str2double(values);
    data_over_time = [data_over_time, numeric_values];
    plot(data_over_time);
    drawnow; % Update the plot immediately
end

I keep getting errors relating to the 'lim' statements. Where am I going wrong? Is there a code that I can create that can procedurally generate the data produced in the Arduino such as with the IDE code above?

I want to get myself familiar with using MATLAB.

Hi @cathalmurphy,

I am not familiar with Mathlab but if you look here

https://www.mathworks.com/help/matlab/ref/ylim.html

it seems as if there is no comma between the tstart and tend data:

image

What happens is you remove the comma in these lines?

figure;
ylim([0  1024]);
xlim([0   length(data_over_time)]);

Good luck, just a quick shot ... :wink:
ec2021

And to this question: There are plenty of possibilities depending on your skills, hardware and OS you are using.

A simple one may be a Python script writing data to a file and use that as an input for Mathlab:

# Program to generate a number of random floats

import random
import time

f = open("randomFloats.txt", "w")

count = 0

while(count < 10):
 capacitance = random.randint(100, 1000) / 100.0
 print(capacitance)
 capString = str(capacitance)+'\n'
 f.write(capString);
 count = count +1
 time.sleep(1)
f.close()

Change the "10 " in while() to how many data you want and remove the "time.sleep(1)" so that it runs at full speed ...

ec2021

P. S .: And how about Mathlab itself? I think it should provide all capabilities to produce random numbers ... see https://www.mathworks.com/help/matlab/math/floating-point-numbers-within-specific-range.html

length(data_over_time) is zero when you call it, so the xlim is 0 to 0

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.