Read serial device and update uitable in loop
显示 更早的评论
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
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!