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

1 回表示 (過去 30 日間)
Robert Demyanovich
Robert Demyanovich 2021 年 9 月 26 日
コメント済み: Star Strider 2021 年 9 月 26 日
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?

回答 (1 件)

Star Strider
Star Strider 2021 年 9 月 26 日
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 件のコメント
Robert Demyanovich
Robert Demyanovich 2021 年 9 月 26 日
編集済み: Robert Demyanovich 2021 年 9 月 26 日
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.
Star Strider
Star Strider 2021 年 9 月 26 日
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.
.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by