Select a record from a table where an exact value is not provided
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
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 个评论
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
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.
.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!