Decreasing the lag on the Android Matlab app sensor data .

5 views (last 30 days)
Hi,I am a total newbie to matlab. I been trying to get sensor data from my android phone. I am getting the data but there is almost a lag of 1 second. here is my code
clear all
close all
clc
connector on;
m = mobiledev();
m.OrientationSensor = 1;
m.Logging = 1;
pause (4)
for c = 1:inf
o = m.Orientation
pause(0.1);
figure(1);
axis tight
hold on
plot(c,o(1),'.');
figure(2);
axis tight
hold on
plot(c,o(2),'.');
figure(3);
axis tight
hold on
plot(c,o(3),'.');
end

Accepted Answer

Walter Roberson
Walter Roberson on 22 May 2015
Your graphics is inefficient and is going to get worse as you increase the number of readings.
for K = 1 : 3
figs(K) = figure(K);
figax(K) = axes('Parent', figs(K));
axis(figax(K),'tight');
pline(K) = plot(figax(K), [], [], '.'); %exists but empty
end
drawnow();
crecord = zeros(4, 512); %pre-allocate for 512 points
for c = 1:inf
o = m.Orientation;
if c > size(crecord,2)
crecord(4,c+255) = 0; %grow the record if it is getting small
end
crecord(4,c) = c; %remember observation number
for K = 1 : 3
crecord(K, c) = o(K); %copy the current data into the record
set(pline(K),'xdata', crecord(4,1:c), 'ydata', crecord(K,1:c)); %update line
end
drawnow(); %repaint screen
end
  1 Comment
Walter Roberson
Walter Roberson on 23 May 2015
It appears to me that as you are not using Logging, each time you ask for the Orientation, MATLAB sends a command to Android to request the data and Android sends back a response. That introduces latency. You can reduce the latency by turning on Logging, in which case the Android will send a stream of readings to MATLAB. For an example, see http://www.mathworks.com/help/supportpkg/mobilesensor/ug/use-logged-sensor-data.html

Sign in to comment.

More Answers (1)

kalvik jakkala
kalvik jakkala on 22 May 2015
Ok so where dose the plot command go?? And I don't think the problem is the code. I mean it's not good but when i enter o = m.Orientation on the command window while moving my phone it takes a while get the latest values. There is a lag of about 1 sec. I was wondering if it is possible to get the data via a usb cable instead of wifi.
  1 Comment
Walter Roberson
Walter Roberson on 23 May 2015
The plot command goes right where I show it, in the loop that builds the figures and associated axes. After that, you do not call plot(): instead you use set() to update the XData and YData properties of the line() objects that were created by the plot() calls. Updating the XData and YData properties so that they have all of the data points will result in faster graphics than having one line() object for every point.

Sign in to comment.

Categories

Find more on MATLAB Mobile in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!