hi, i want to do real time plot via gui. I'm getting the data from Arduino, but I can't plot, where am I missing my code is as follows. -Best regards.

4 visualizaciones (últimos 30 días)
% --- Executes on button press in Connect.
function Connect_Callback(hObject, eventdata, handles)
% hObject handle to Connect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user ggg (see GUIDATA)
handles.s=serial('COM3','BaudRate',9600);
handles.s.BytesAvailableFcn = {@ReadDataOverSerial,handles};
handles.s.BytesAvailableFcnCount=12;
handles.s.BytesAvailableFcnMod='byte';
fopen(handles.s);
guidata(hObject, handles);
function ReadDataOverSerial(hObject, eventdata, handles)
global a b ;
bytes=fread(handles.s,[1,handles.s.BytesAvailable]);
set(handles.datavalues,'String',char(bytes));
timer = bytes(end-5:end); %creating new speacial array
response = bytes(1:5); %creating new speacial array
timer = str2double(timer);
response = str2double(response);
a = [a timer];
b = [b response];
plot(handles.axes1,a,b);

Respuestas (1)

Chetan
Chetan el 29 de Feb. de 2024
Editada: Chetan el 29 de Feb. de 2024
I understand you're facing issues with real-time plotting in a MATLAB GUI using data from an Arduino. Let's address the key points to resolve this:
  1. Initialization: Confirm `a` and `b` are initialized as empty before data collection starts.
  2. Data Handling: Ensure `timer` and `response` are converted to numbers without errors.
  3. Plotting: After updating `a` and `b`, use `plot(handles.axes1, a, b);` followed by `drawnow;` to refresh the plot.
Here's a updated version of your `ReadDataOverSerial` function:
function ReadDataOverSerial(hObject, ~, handles)
global a b;
a = []; b = []; % Initialize if necessary
if handles.s.BytesAvailable > 0
bytes = fread(handles.s, handles.s.BytesAvailable);
timer = str2double(char(bytes(end-5:end)));
response = str2double(char(bytes(1:5)));
if ~isnan(timer) && ~isnan(response)
a = [a timer]; b = [b response];
plot(handles.axes1, a, b); drawnow;
end
end
end
Refer to the following MathWorks Documentation:
Thanks
Chetan Verma

Categorías

Más información sobre Arduino Hardware en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by