How to search for previous values in a table?
2 views (last 30 days)
Show older comments

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?
Accepted Answer
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_timestamps = find(data1.sep == '$GPRMC') % find all of the timestamp indices
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))];
end
data{i} = current_data;
end
data
2 Comments
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.
More Answers (0)
See Also
Categories
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!