2016-07-15 121 views
0

我使用tcpip函數連接我的iPhone和matlab。使用tcpip連接與matlab實時繪製數據

我需要實時繪製數據(並在對數據進行一些計算之前)。 我使用real_time_data_stream_plotting.m,我在互聯網上找到。

%% Real Time Data Stream Plotting Example 
function real_time_data_stream_plotting 
%% 
% This example demonstrates how to automatically read a set number of data bytes as and 
% when they are available. This MATLAB(R) script also generates a real time plot of streaming 
% data collected from the TCPIP server. 
% 
% The script may be updated to use any instrument/device/TCPIP server 
% to collect real time data. You may need to update the IP address and 
% port. 
% 
% To generate a report of this entire script, you may use the PUBLISH 
% command at the MATLAB(R) command line as follows: 
% 
% publish(real_time_data_plot); 

% Author: Ankit Desai 
% Copyright 2010 - The MathWorks, Inc. 

%% Create the interface object 
% Create a TCPIP object listening to port 19 (Character Generator). 
% 
% *Note* : To enable character generator service at port 19 on a Windows platform, enable: 
% 
% Control Panel > Add Remove Programs > Add/Remove Windows Component > Networking Services 
% 
interfaceObject = tcpip('192.168.1.111',52928); 

%% 
% Setup a figure window and define a callback function for close operation 
figureHandle = figure('NumberTitle','off',... 
    'Name','Live Data Stream Plot',... 
    'Color',[0 0 0],... 
    'CloseRequestFcn',{@localCloseFigure,interfaceObject}); 

%% 
% Setup the axes 
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'); 

%% 
% Initialize the plot and hold the settings on 
hold on; 
plotHandle = plot(axesHandle,4,'-y','LineWidth',1); 

%% Setup interface object to read chunks of data 
% Set the number of bytes to read at a time 
bytesToRead = 500; 

%% 
% Define a callback function to be executed when desired number of bytes 
% are available 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); 

%% 
% Update the plot 
set(figureHandle,'Ydata',data); 

%% Implement the close figure callback 
function localCloseFigure(figureHandle,~,interfaceObject) 

%% 
% Clean up the interface object 
fclose(interfaceObject); 
delete(interfaceObject); 
clear interfaceObject; 

%% 
% Close the figure window 
delete(figureHandle); 

我的問題是我有一個實時陰謀,但我不知道我正在繪製哪些數據。我知道從iPhone到達的數據是一個有62列的矩陣(如果我直接從iPhone導出數據,我會得到62列的.csv文件)。

我該如何選擇第一列圖?

非常感謝!

+0

那麼,數據是你發送的? – Suever

回答

1

這裏將是我拿的索引數據只爲需要的列:

function real_time_data_stream_plotting() 

     % These can be very well arguments for the function 
     FRAME_SIZE = 62;  % This many columns (each columns is one byte) 
     FRAME_COUNT = 500; % This much data to plot at once 
     DATA_COLUMN = 3  % Position of the plotted column 

     % Create connection 
     conn = tcpip('192.168.1.111', 52928); 

     % Set-up graphics 
     hf = figure(... 
      'NumberTitle',  'off', ... 
      'Name',   'Live Data Stream Plot', ... 
      'Color',   [0 0 0], ... 
      'CloseRequestFcn', {@localCloseFigure,conn} ... 
     ); 

     ha = axes(... 
      'Parent', hf,... 
      'YGrid', 'on', ... 
      'YColor', [0.9725 0.9725 0.9725], ... 
      'XGrid', 'on', ... 
      'XColor', [0.9725 0.9725 0.9725], ... 
      'Color', [0 0 0] ... 
     ); 
     hold(ha, 'on'); 
     xlabel(ha,'Number of Samples'); 
     ylabel(ha,'Value'); 

     hl = plot(ha, 4, '-r', 'LineWidth', 1); 

     % Set-up connection callback 
     conn.BytesAvailableFcn  = {@update_plot, hl, FRAME_SIZE, FRAME_COUNT, DATA_COLUMN}; 
     conn.BytesAvailableFcnMode = 'byte'; 
     conn.BytesAvailableFcnCount = FRAME_SIZE * FRAME_COUNT; 

     % Open connection and exit 
     fopen(conn); 
     pause(3); 
     snapnow; 

end 


% -------- Local Functions -------- 

function update_plot(conn, ~, hl, frame_size, frame_count, data_column) 

     data = fread(conn, frame_count*frame_size); 
     data = data(data_column + frame_size*(0:frame_count-1)); 
     set(hl, 'YData', data); 

end 


function clean_up_on_close(hf, ~, conn) 

     fclose(conn); 
     delete(conn); 
     clear conn; 
     delete(hf); 

end 

不過,我不覺得基於版權作品舒適的郵政代碼...

+0

非常感謝您的回答..對於版權工作抱歉,我不知道如何解釋代碼..好吧,我得到這個錯誤:錯誤使用icinterface/fopen (第83行) 未成功的開放:操作定時 輸出 錯誤 real_time_data_stream_plotting (第39行) fopen(conn);即使IP和端口是正確的..謝謝。 @ CST-鏈接 – YLM