get real-time new coming sensor data from mobile phone, old sensor data is not include?

2 次查看(过去 30 天)
I want to get real-time sensor data using mobile phone,
% Initialize the mobile device object
m = mobiledev;
% Enable the accelerometer sensor
m.AccelerationSensorEnabled = 1;
% Start logging accelerometer data
m.Logging = 1;
% Set the figure for real-time plotting
figure;
duration = 15; % Duration of data collection in seconds
startTime = tic; % Start the timer
% Loop to collect and display data in real-time for 15 seconds
while toc(startTime) < duration
[accelData, timeStamps] = accellog(m); % Retrieve accelerometer data and timestamps
if ~isempty(accelData)
% Plot X, Y, Z axis acceleration in different colors
plot(timeStamps, accelData(:, 1), 'r'); hold on;
plot(timeStamps, accelData(:, 2), 'g');
plot(timeStamps, accelData(:, 3), 'b');
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
legend('X-axis', 'Y-axis', 'Z-axis');
drawnow; % Update the plot in real-time
hold off;
end
pause(0.1); % Pause for smooth plotting
end
% Stop logging after the duration
m.Logging = 0;
% Optionally, retrieve all the logged data for further processing
[accelData, timeStamps] = accellog(m);
The function accellog(m) returns all logged accelerometer data from the start of the logging session, including both old and new data, in every call. It does not return only the new sensor data since the last call.
This means that in the code you provided, each time accellog(m) is called inside the loop, it will return the complete set of logged accelerometer data from the moment m.Logging = 1 was set, up to the current time.
How can I get the new coming sensor data? (logging=0 , logging=1 again is not a good way)

回答(1 个)

Shubham
Shubham 2024-9-15
Hey Jun,
I understand that when you use the function "accellog(m)", it returns the log data from pevious logging sessions as well. This is because the log functions are used to get the entire log of all the readings. Please check the 12th point from the following documentation of Sensor Data Streaming: https://www.mathworks.com/help/matlabmobile/ug/sensor-data-streaming-tutorial.html#:~:text=the%20entire%20log%20of%20all%20readings
While logging the data, you can access the current value of the sensor by accessing its properties such as:
m.Acceleration
I would recommend to first collect all the required data by enabling the logging session, do your processing upon it and then delete the object.
I hope this was helpful!

类别

Help CenterFile Exchange 中查找有关 Sensor Data Collection 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by