필터 지우기
필터 지우기

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 (최근 30일)
% --- 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);

답변 (1개)

Chetan
Chetan 2024년 2월 29일
편집: Chetan 2024년 2월 29일
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

카테고리

Help CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by