Read serial device and update uitable in loop
Afficher commentaires plus anciens
Hi,
I have 24 sensors hooked up to an Arduino. The Arduino is programmed to print the reading of every sensor in one comma-separated line on every loop. The MATLAB script reads the line and update the table with the sensor readings.
The script works when I print the data to the command window and the sensors can be seen responding in real time. But, for some reason, when I add a UITable the script does show updated readings on every loop, but the sensors don't seem to respond to any changes (not in the table and not even in the command window). I've tried adding a delay, didn't work. Does anyone know?
clc
clear all;
close all;
% Connect to Arduino
s = serialport("COM5",115200); % connect to serial device (arduino)
% fid = fopen("yourfilename.txt",'a'); %create file for saving data
b = zeros(24); % create empty array for storing sensor data
% Create UI table
fig = uifigure;
uit = uitable(fig,"Data",b);
uitablehandle = findall(fig,'type','uitable');
% Read and print sensor data
while 1
sensordata = readline(s); % read arduino output
b = cell2mat(cellfun(@str2num,sensordata,'uniform',0)) % convert data to array
set(uitablehandle,'data',b'); % update table
drawnow;
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur MATLAB Support Package for Arduino Hardware dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!