Select a record from a table where an exact value is not provided
Show older comments
I have a table which I will be loading with Excel data (I haven't ever done that, but I think I can just paste data in).
My question is on how to select a record. The first value in a row from the table will contain times and will be the "key" for selection. Matlab will be running a simulation where the time advances for each iteration. The problem is that the time in the iteration won't exactly match the time listed in column 1 of the row in the table. Does Matlab have an easy command for selecting the table row which has the closest value in column 1 (time) when provided with the time from the iteration?
Answers (1)
Star Strider
on 26 Sep 2021
0 votes
This is a bit ambiguous.
If the simulation is a system of differential equations, the time vector (‘tspan’ in the documentation) can be made to match the data exactly simply by apssing the time vector to the differential equation solver as the ‘tspan’ vector.
As for importing the Excel file, use readtable or readmatrix (or xlsread for older MATLAB releases.)
.
6 Comments
Robert Demyanovich
on 26 Sep 2021
Star Strider
on 26 Sep 2021
I still don’t know what simulation you’re running, or how you’re running it.
If it’s not a system of differential equations (such that my original Answer would work), another option is to interpolate the simulation results to the Excel table times using the interp1 function (or other appropriate interpolation functions). That way, there would be an exact match between the simulation and the Excel data.
In that instance, it would be necessary to be certain that interp1 is only doing interpolation, not extrapolation, since extrapolation creates problems with respect to interpreting the accuracy of the extrapolated values.
.
Robert Demyanovich
on 26 Sep 2021
Edited: Robert Demyanovich
on 26 Sep 2021
Star Strider
on 26 Sep 2021
I’m still not certain what you’re doing, or how the simulation is calculated. The interpolation would be as accurate as the time values themselves, so use whatever precision you want for those.
Interpolation would be more efficient and more precise than find or any of the others. However if you want to use that sort of procedure, the ismembertol funciton would likelly be best.
.
Robert Demyanovich
on 26 Sep 2021
Edited: Robert Demyanovich
on 26 Sep 2021
An interpolation approach would be something like this — .
t_expt = [0
4.61105523094832e-06
9.22211046189664e-06
1.38331656928450e-05
1.84442209237933e-05];
t_sim = [ 0.00
1.60E-08
3.20E-08
4.80E-08
6.40E-08
8.00E-08
9.60E-08
1.12E-07
1.28E-07
1.44E-07
1.60E-07
1.76E-07
1.92E-07
2.08E-07
2.24E-07
2.40E-07
2.56E-07
2.72E-07
2.88E-07
3.04E-07
3.20E-07
3.36E-07
3.52E-07
3.68E-07
3.84E-07
4.00E-07
4.16E-07
4.32E-07
4.48E-07
4.64E-07];
v_expt = 0:numel(t_expt)-1; % Create Numeric Vector For Demonstration Purposes
v_sim = interp1(t_expt, v_expt, t_sim) % Interpolate Experiment Times To Simulation Times
figure
loglog(t_expt, v_expt, 'xb')
hold on
plot(t_sim, v_sim, '+r')
hold off
grid
legend('Experiment','Simulation', 'Location','best')
An interpolation approach would appear to be the only viable option. I doubt that ismembertol or any other comparison on indexing approach would work in this context.
.
Categories
Find more on Logical 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!