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.
Afficher commentaires plus anciens
% --- 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);
Réponses (1)
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:
- Initialization: Confirm `a` and `b` are initialized as empty before data collection starts.
- Data Handling: Ensure `timer` and `response` are converted to numbers without errors.
- 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:
- https://www.mathworks.com/help/matlab/serial-port-devices.html
- https://www.mathworks.com/help/matlab/ref/drawnow.html
Thanks
Chetan Verma
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!