Hi I'm trying to plot TCP data in real time and I've modified example code I found here on mathworks. The problem is when I run the example code (and a version I modified) it doesn't update the graph although I can tell it is streaming data. Anyone have a idea why the graph isn't updating? Here is the code I'm working with (with my own minor changes):
%Orginal Code Author Ankit Desai
%Copyright 2010 - The MathWorks, Inc.
function real_time_data_stream_plotting interfaceObject = tcpip('18.111.48.184',23); figureHandle = figure('NumberTitle','off',... 'Name','Live Data Stream Plot',... 'Color',[0 0 0],... 'CloseRequestFcn',{@localCloseFigure,interfaceObject}); axesHandle = axes('Parent',figureHandle,... 'YGrid','on',... 'YColor',[0.9725 0.9725 0.9725],... 'XGrid','on',... 'XColor',[0.9725 0.9725 0.9725],... 'Color',[0 0 0]); xlabel(axesHandle,'Number of Samples'); ylabel(axesHandle,'Value'); ylim([0 4096]); %Initialize the plot and hold the settings on
hold on; plotHandle = plot(axesHandle,0,'-y','LineWidth',1); bytesToRead = 31; %Define a callback function to be executed when desired number of
%bytes are availabe in the input buffer
interfaceObject.BytesAvailableFcn ={@localReadAndPlot,plotHandle,bytesToRead}; interfaceObject.BytesAvailableFcnMode = 'byte'; interfaceObject.BytesAvailableFcnCount = bytesToRead; %Open the interface Object
fopen(interfaceObject); pause(3); snapnow; %Implement the bytes available CallBack
function localReadAndPlot(interfaceObject,~,figureHandle,bytesToRead) % Read the desired number of data bytes
data = fread(interfaceObject,bytesToRead); dataStr = char(data(1:end-2)') % remove the /n /r characters and convert it to a string
dataNum = sscanf(dataStr,'%d,%d,%d,%d,%d,%d'); % convert the string into numbers
% Update the plot
set(figureHandle,'Ydata',dataNum(1)) %Implement the close figure callback
function localCloseFigure(figureHandle,~interfaceObject) fclose(interfaceObject); delete(interfaceObject); clear interfaceObject; delete(figureHandle)
EDIT: I managed to get it to plot the data real time by changing the dataNum(1) to dataNum.
Best Answer