Clear Filters
Clear Filters

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.

1 view (last 30 days)
% --- 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);

Answers (1)

Chetan
Chetan on 29 Feb 2024
Edited: Chetan on 29 Feb 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

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!