How to plot a real time signal with axes automatically updating
15 ビュー (過去 30 日間)
古いコメントを表示
Hi all, i have an analog signal coming from the usb port of my PC, and i save it in Matlab using fread(serial...); i want to plot them in real time as soon as i acquire them, of course updating the time axis (seconds vs volts). How can i do this? Note that i get a sample every 100 ms! Thanks.
2 件のコメント
Geoff Hayes
2016 年 5 月 14 日
Alessandro - how are you reading the data from your USB port? Do you have a timer that periodically (every 100 ms) polls the port for new data? Have you created a GUI that initiates this process and will attempt to update an axes within that GUI? Please provide more context and a sample of your code so that we can get an idea of how to best fit the solution for your needs.
採用された回答
Geoff Hayes
2016 年 5 月 14 日
編集済み: Geoff Hayes
2016 年 5 月 17 日
Alessandro - you could use a timer started from within your GUI that would, every 100 milliseconds, read from the bluetooth development board. You can set up your timer in such a way that it can access the handles object of the GUI so that if data has been read, it can be drawn on your axes.
For example, suppose that your timer starts when you press the start button in your GUI. The pushbutton callback would look something like
function start_Callback(hObject, eventdata, handles)
handles.timer = timer('Name','MyTimer', ...
'Period',0.1, ...
'StartDelay',0, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',{@timerCallback,handles.figure1});
guidata(hObject,handles);
start(handles.timer);
The above timer will fire (roughly) every 100 milliseconds. Note how we pass the GUI figure handle as an input to the timerCallback function. This is necessary so that we can access the handles structure (from there) to (potentially) update the axes with new data. The callback can be defined as
function [] = timerCallback(~,~,guiHandle)
if ~isempty(guiHandle)
% get the handles
handles = guidata(guiHandle);
if ~isempty(handles)
% query your bluetooth board using your code from the while loop
% if new data, then update the axes
end
end
In the above, we get the handles structure using guihandles so that we can get the current set of data in the axes control. In your stop button callback, you would then stop the timer
function stop_Callback(hObject, eventdata, handles)
if isfield(handles, 'timer')
stop(handles.timer);
end
When it comes to the code to update the plot with the new data, you can continue as before but bear in mind that every time you call plot you are creating a new graphics object. Which is a lot given that you do this ten times a second. An alternative is to plot once and update it's x and y data every time you have something to plot. In your start_Callback, create the object as
function start_Callback(hObject, eventdata, handles)
handles.timer - ...;
handles.hPlot = plot(NaN,NaN); % creates the graphics object with no data
guidata(hObject,handles);
start(handles.timer);
Then in the timer callback, update this plot as
xdata = [get(handles.hPlot,'XData') handles.T(end)];
ydata = [get(handles.hPlot,'YData') handles.array(end)];
set(handles.hPlot,'XData',xdata,'YData',data);
The above assumes that you are saving your updated T and array data to handles with the new data.
Try implementing the above and see what happens!
EDIT In the above, I replaced the
% get the handles
handles = guihandles(guiHandle);
with
% get the handles
handles = guidata(guiHandle);
so that we get back both the handles to the objects and any user-defined data.
8 件のコメント
Mauricio Huacho
2016 年 8 月 10 日
編集済み: Mauricio Huacho
2016 年 8 月 10 日
Hi, i'm trying t use your code to create a GUI to connect a bluetooth device for read data and plot in real time in the GUI but i have a problem with this code, can you help me Geoff Hayes????
Geoff Hayes
2016 年 8 月 10 日
Mauricio - please create a new question and describe what your code is doing and what the problem is.
その他の回答 (1 件)
Shivaputra Narke
2016 年 5 月 14 日
Hi,
Using loops is not good idea in your case.
You can use the BytesAvailableFcn callback to update the plot with new set of bytes received.
参考
カテゴリ
Help Center および File Exchange で Source Control についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!