Select a record from a table where an exact value is not provided

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)

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

I'm new to Matlab and running simulations in general. I wondered about what you are saying. But I've been running the simulation without this table that I mentioned. I've noticed that there are times where the simulation doesn't work correctly because the time step is too large. The table is likely to have 1000 to 1500 rows. The simulation typically runs with a time iteration of 1 to 100,000. I realize that around 8 to 10 iteration steps will select the same row from the table and I believe that will be okay. But I don't think it is possible to match up the data with the time vector.
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.
.
It is Fick's law of diffusion. There will be around 4 to 5 simulation iterations (I think) that will use the same record in the table. For each case that I want to evaluate, the values in the table will change and it is very likely that the values for times in the table will be different from one case to the next. Also from case to case, the number of rows will vary. It sure would be nice if there was something a bit more "fuzzy" for selecting the row. How many places behind the decimal point do I need to get exactly correct? If it is something like 8 or 16, it might be very difficult to get an exact match. I'm looking for Find or Search function that will select the closest value.
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.
.
Here's an example. From the imported table of experimental data, the first five values of "time" are as follows;
0
4.61105523094832e-06
9.22211046189664e-06
1.38331656928450e-05
1.84442209237933e-05
From the simulation, here are the first thirty values of "time":
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
So I was incorrect in the 8 to 10 or 4 to 5 that I mentioned earlier. All of these values of the simulation time would need to use the experimental data value at time = 0. Not until the simulation time step is greater than 2.3 E-06 would the selected row from the table containing experimental data be the second row where t = 4.61105523094832e-06. I say this based on rounding the simulation time to the nearest experimental time.
Also from case to case, the values of the simulation times and the experimental times will be different.
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
v_sim = 30×1
0 0.0035 0.0069 0.0104 0.0139 0.0173 0.0208 0.0243 0.0278 0.0312
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.
.

Sign in to comment.

Tags

Asked:

on 26 Sep 2021

Commented:

on 26 Sep 2021

Community Treasure Hunt

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

Start Hunting!