How to add numerical timestamp to first column in an array
Show older comments
Initially I added a counter function and this worked a treat. However, several different sensors are all going to feed data to matlab and therefore a timestamp is required.
I have already tried numerous different methods, but none work.
The code I used for the counter is as follows (excuse the unwieldy coding for I'm not an advanced user like a lot of people here are):
% Read COM port using Matlab
% Start fresh
clc
clear all
% First delete the association COM4 has to any other software
delete(instrfind({ 'Port'},{'COM4'}));
% Set COM port to a variable
GPSport = serial('COM4');
% Open the COM port
fopen(GPSport);
% Set baudrate to 9600
GPSport.Baudrate = 9600;
% Set samples to chosen value
samples = 10;
% Set initial sample value to 1
sample_value = 1;
% Create arrays for efficincy
GPS_GPRMC_long_deg_array = zeros(samples,2);
GPS_GPRMC_long_min_array = zeros(samples,2);
GPS_GPRMC_lat_deg_array = zeros(samples,2);
GPS_GPRMC_lat_min_array = zeros(samples,2);
GPS_GPRMC_v_array = zeros(samples,2);
% While loop
while sample_value <= samples;
% Read data from device and format as text
GPS_GPRMC = fscanf(GPSport,'%s');
% Convert string to numerical values
GPS_GPRMC_num = double(GPS_GPRMC);
% Select longitudinal coordinates and convert back to characters and then
% numerical
GPS_GPRMC_long_deg = GPS_GPRMC_num(20:21);
GPS_GPRMC_long_deg_char = char(GPS_GPRMC_long_deg);
GPS_GPRMC_long_deg_num = str2double(GPS_GPRMC_long_deg_char);
GPS_GPRMC_long_deg_array(sample_value,1) = sample_value;
GPS_GPRMC_long_deg_array(sample_value,2) = GPS_GPRMC_long_deg_num;
GPS_GPRMC_long_min = GPS_GPRMC_num(22:29);
GPS_GPRMC_long_min_char = char(GPS_GPRMC_long_min);
GPS_GPRMC_long_min_num = str2double(GPS_GPRMC_long_min_char);
GPS_GPRMC_long_min_array(sample_value,1) = sample_value;
GPS_GPRMC_long_min_array(sample_value,2) = GPS_GPRMC_long_min_num;
% Select latitudinal coordinates and convert back to characters and then
% numerical
GPS_GPRMC_lat_deg = GPS_GPRMC_num(33:35);
GPS_GPRMC_lat_deg_char = char(GPS_GPRMC_lat_deg);
GPS_GPRMC_lat_deg_num = str2double(GPS_GPRMC_lat_deg_char);
GPS_GPRMC_lat_deg_array(sample_value,1) = sample_value;
GPS_GPRMC_lat_deg_array(sample_value,2) = GPS_GPRMC_lat_deg_num;
GPS_GPRMC_lat_min = GPS_GPRMC_num(36:43);
GPS_GPRMC_lat_min_char = char(GPS_GPRMC_lat_min);
GPS_GPRMC_lat_min_num = str2double(GPS_GPRMC_lat_min_char);
GPS_GPRMC_lat_min_array(sample_value,1) = sample_value;
GPS_GPRMC_lat_min_array(sample_value,2) = GPS_GPRMC_lat_min_num;
% Select speed and convert back to characters and then numerical
GPS_GPRMC_v = GPS_GPRMC_num(47:51);
GPS_GPRMC_v_char = char(GPS_GPRMC_v);
GPS_GPRMC_v_num = str2double(GPS_GPRMC_v_char);
GPS_GPRMC_v_array(sample_value,1) = sample_value;
GPS_GPRMC_v_array(sample_value,2) = GPS_GPRMC_v_num;
data = [GPS_GPRMC_long_deg_num,GPS_GPRMC_long_min_num,GPS_GPRMC_lat_deg_num,GPS_GPRMC_lat_min_num,GPS_GPRMC_v_num];
disp(data)
sample_value = sample_value + 1;
end
One of the attempts I made is as follows:
% Read COM port using Matlab
% Start fresh
clc
clear all
% First delete the association COM4 has to any other software
delete(instrfind({ 'Port'},{'COM4'}));
% Set COM port to a variable
GPSport = serial('COM4');
% Open the COM port
fopen(GPSport);
% Set baudrate to 9600
GPSport.Baudrate = 9600;
% Set duration of the loop in seconds
numSec = 5;
% Set samples to chosen value
samples = numSec;
% Start stopwatch to start the loop
stopwatch = tic;
% Create arrays for efficincy
GPS_GPRMC_long_deg_array = zeros(samples,2);
GPS_GPRMC_long_min_array = zeros(samples,2);
GPS_GPRMC_lat_deg_array = zeros(samples,2);
GPS_GPRMC_lat_min_array = zeros(samples,2);
GPS_GPRMC_v_array = zeros(samples,2);
% While loop
while (toc(stopwatch)<=numSec)
% Read data from device and format as text
GPS_GPRMC = fscanf(GPSport,'%s');
% Convert string to numerical values
GPS_GPRMC_num = double(GPS_GPRMC);
timestamp = logical(now);
% Select longitudinal coordinates and convert back to characters and then
% numerical
GPS_GPRMC_long_deg = GPS_GPRMC_num(20:21);
GPS_GPRMC_long_deg_char = char(GPS_GPRMC_long_deg);
GPS_GPRMC_long_deg_num = str2double(GPS_GPRMC_long_deg_char);
GPS_GPRMC_long_deg_array(timestamp,1) = timestamp;
GPS_GPRMC_long_deg_array(timestamp,2) = GPS_GPRMC_long_deg_num;
GPS_GPRMC_long_min = GPS_GPRMC_num(22:29);
GPS_GPRMC_long_min_char = char(GPS_GPRMC_long_min);
GPS_GPRMC_long_min_num = str2double(GPS_GPRMC_long_min_char);
GPS_GPRMC_long_min_array(timestamp,1) = timestamp;
GPS_GPRMC_long_min_array(timestamp,2) = GPS_GPRMC_long_min_num;
% Select latitudinal coordinates and convert back to characters and then
% numerical
GPS_GPRMC_lat_deg = GPS_GPRMC_num(33:35);
GPS_GPRMC_lat_deg_char = char(GPS_GPRMC_lat_deg);
GPS_GPRMC_lat_deg_num = str2double(GPS_GPRMC_lat_deg_char);
GPS_GPRMC_lat_deg_array(timestamp,1) = timestamp;
GPS_GPRMC_lat_deg_array(timestamp,2) = GPS_GPRMC_lat_deg_num;
GPS_GPRMC_lat_min = GPS_GPRMC_num(36:43);
GPS_GPRMC_lat_min_char = char(GPS_GPRMC_lat_min);
GPS_GPRMC_lat_min_num = str2double(GPS_GPRMC_lat_min_char);
GPS_GPRMC_lat_min_array(timestamp,1) = timestamp;
GPS_GPRMC_lat_min_array(timestamp,2) = GPS_GPRMC_lat_min_num;
% Select speed and convert back to characters and then numerical
GPS_GPRMC_v = GPS_GPRMC_num(47:51);
GPS_GPRMC_v_char = char(GPS_GPRMC_v);
GPS_GPRMC_v_num = str2double(GPS_GPRMC_v_char);
GPS_GPRMC_v_array(timestamp,1) = timestamp;
GPS_GPRMC_v_array(timestamp,2) = GPS_GPRMC_v_num;
data = [GPS_GPRMC_long_deg_num,GPS_GPRMC_long_min_num,GPS_GPRMC_lat_deg_num,GPS_GPRMC_lat_min_num,GPS_GPRMC_v_num];
disp(data)
end
I also tried an attempt using the method found at: http://programming-tips-and-tricks.blogspot.nl/2012/10/matlab-timestamp.html
This didn't work either.
Several problems occur. Firstly, I get a lot more zeros placed in the array. It becomes much longer than using a counter (which is set to 10). The data feed operates at a frequency of 1 Hertz so the stopwatch to 5 seconds displays exactly what I want (see end of code), but the arrays are filled with utter nonsense (zeros) and occasionally the correct data.
I've been at this for days trying different methods of formats and conversions, but nothing so far has worked.
Thanks in advance for any help!
Answers (1)
What do you expect logical() to perform in this line:
timestamp = logical(now)
logical() replies false if the input is exactly 0, and true otherwise. Therefore it is not useful for time stamps.
It is not getting clear to me, where you want to inster the timestamp. Using it as index is most likey a bad idea.
[EDITED] So what about omitting the logical() and using the original reply of now? But why does the time-stamp appear as index in e.g.:
GPS_GPRMC_long_deg_array(timestamp,1) = timestamp;
Seeing the failing code only does not allow us to guess, what you want to achieve. Therefore I've asked the question, what the logical() in this example is thought for. Please answers such question, because this information is required to create a better answer.
I add time-stamps in this way:
nPoint = 1000;
Result = zeros(mPoint, 2);
for iPoint 1:nPoint
value = <obtain value from device>;
Result(iPoint, 1) = now;
Result(iPoint, 2) = value;
end
Unfortuantely your question contains a lot of code, which cannot be understood by the readers. Therefore it is not clear to me, how this small example can be applied to your program. I even do not see a loop in your code.
2 Comments
Chris Patterson
on 3 Jun 2013
Jan
on 3 Jun 2013
I assume, that you post an example to demonstrate anything. If I mention the problem in the example, I've been convinced that this helps you to adjust the example until it satisfies your needs. I do not know another method to try to post "useful" answers.
See [EDITED].
Categories
Find more on Loops and Conditional Statements 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!