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?
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 ...