How to find values in a table

Hi,
I have a question, lets say I have a table like this
In matlab I want to find/call a value in column 3 corresponding to say 100 in first column. Not only that, but I also want to use values not in the table (say for 95 in the first column) using linear interpolation. I want to be able to do this using any of the columns. How can I achieve this in MATLAB? Please help.

 Accepted Answer

%let your table be named T
column_with_key = 1;
column_to_examine = 3;
value_to_lookfor = 100;
value_to_extrapolate = 95;
mask = T.(column_with_key) == value_to_lookfor;
wanted_value1 = T.(column_to_examine)(mask)
wanted_value2 = interp1(T.(column_with_key), T.(column_to_examine), value_to_extrapolate)
In practice it is usually shorter than this, such as just
wanted_value = interp1(T.var1, T.var3, 95)

6 Comments

Great! Thank you very much, after I import the data into matlab, I am able to use the shorter version and get the correct answer.
Thank You
Poseidon
Poseidon on 5 Nov 2023
Edited: Poseidon on 5 Nov 2023
I have one more question. Lets say I have a table of something like this(I am only showing a snippet, the table goes on like this for 10000 or so rows with repeating values in column 1 )
If I want to find a value in column 3 for a particular value in column 1 and column 2, how do I do that? I tried interp2, but it didn't work. I got an error - The number of input coordinate arrays must match the dimensions of the sample values. This was the line I used - interp2(SubProp.Pressure,SubProp.SubEntr,SubProp.SubEnth,P,s1)
Would you require an exact match of column 1 in this case? If so then extract a subset first,
subset = T(T.var1 == 1,:);
wanted_value = interp1(subset.var2, subset.var3, 95)
If you have a number of different combinations to look up with different values of the first column, then you could use findgroups() or the three-output version of ismember() to create a subset per group, then loop over subsets.
This is what I initially tried to do (not as simple as you did) and tried getting the value I needed but I ran into a roadblock. Just to make it easier to ask here, I am explaining the table I shared in the comment above - variables are pressure, temperature, enthalpy and entropy. So lets say I want the value of the enthalpy at a particular entropy and pressure (say 1279pa), how will I achieve that? Using the subset method with pressure as the variable, will not work. Because in the tables there is no 1279pa.
I initially thought to interpolate all the values for 1279pa (at that pressure, at all the temperatures, all the enthalpies and entropies) and then seperate that in a different subset and then find the value I need using interp1. But I feel like there might be an easier method. It might be difficult to generate a different subset using interpolation at every different pressure.
Thank you again for helping
F = scatteredInterpolant(T.pressure, T.entropy, T.enthalpy);
interpolated_enthalpies = F(query_pressures(:), query_entropys(:));
Poseidon
Poseidon on 5 Nov 2023
Edited: Poseidon on 5 Nov 2023
Thank you! This worked. But I think I have to use the previous answer (the subset) because for some cases its giving a wrong answer (like finding the enthalpy using entropy for a pressure).

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation of 2-D Selections in 3-D Grids in Help Center and File Exchange

Products

Release

R2020b

Tags

Community Treasure Hunt

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

Start Hunting!