How to search for previous values in a table?

2 views (last 30 days)
In the above table, I have GPS time inputs as well as sensor readout values. Every value in column 1 named '$GPRMC' has a year timestamp in column 10. And after that command there are a bunch of sensor readouts until the next GPS timestamp updates.
I want to pick out all of the values in column 5 that have a 'T' (column 3, 'sensor'). I also want to add the nearest year timestamp from the '$GPRMC' reading nearest every 'T' sensor value and append that year to a new arrray for every sensor value until a new '$GPRMC' value comes in. This way there are an equal number of year values and sensor values so I can plot them against each other.
I've managed to pick out all 'T' sensor values by using:
trows = data.sensor=='T';
But I have no idea how to search for the nearest row with column 1 value of '$GPRMC' and append to an array the year timestamp in column 10 for every row entry with 'T' in it.
I guess basically I want to find out how to search above in the file until it finds a '$GPRMC' entry.
How are some different ways I can achieve this?
Jackson Greaves
Jackson Greaves on 31 Aug 2022
To add more context:
The date will eventually update, so even if the date is the same, it needs to pull the "nearest' date so it can capture an update appropriately.
This is a very small sample of ~3 millionx16 size array that takes up a huge amount of memory. True I can probably filter out a good amount of the file though. The rest of the file looks exactly like this just repeated for 24 hours worth of time.
Ideally my output would be this:
The date stamp from data1.VarName10 under a '$GPRMC' to be appended with the hour timestamp from data1.time under a 'T' sensor value row.
So I end up with one array of [append(data1.VarName10,data1.time)] for every sensor "X" value. With the VarName10 data just being a repeat for every sensor value until the year timestamp comes back in.

Sign in to comment.

Accepted Answer

Les Beckham
Les Beckham on 31 Aug 2022
Edited: Les Beckham on 1 Sep 2022
This should do what you want. Note that the cell array in this case only has one cell because there are only two timestamp rows in your sample data and all of the sensor == T rows are between those two timestamps.
load('sample table.mat');
idx_T = find(data1.sensor == 'T') % find all of the sensor == T indices
idx_T = 9×1
9 14 19 24 29 34 39 44 49
idx_timestamps = find(data1.sep == '$GPRMC') % find all of the timestamp indices
idx_timestamps = 2×1
6 53
timestamps = data1.VarName10(idx_timestamps); % extract the timestamps
% Extract the data and timestamps into a cell array
for i = 1:numel(timestamps)-1
idx_current_data = idx_T > idx_timestamps(i) & idx_T < idx_timestamps(i+1);
for k = 1:numel(idx_current_data)
% get the data for the current timestamp
current_data(k,:) = [timestamps(i) data1.X(idx_current_data(i))];
data{i} = current_data;
data = 1×1 cell array
{9×2 double}
Les Beckham
Les Beckham on 1 Sep 2022
Since data1.time and data1.VarName10 are both doubles (numbers), append would not be the right way to combine them. You will have to figure out what those numbers represent to figure out how to add them together.

Sign in to comment.

More Answers (0)


Find more on Matrices and Arrays 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!

Translated by