Interpolate, find - excel lookup equivalent

I completed an excercise many years ago using Excel for a university task.
I recently started using Matlab and would like to repeat the task, but I am stuck and Im not sure what function to use.
I have a variable t, but now I need to find what range t lies within to extract a corresponding value. In excel the function I used was lookup, where it would look for the value in the table.
With Matlab, im not sure if it is interpolate, or find. I tried with interpolate and couldnt get it to work using this below, it could be that I have used this incorrectly or due to the format of the table. Any ideas??
c = interp1(Flange(1:4,1:2), Flange(1:4,3), t);
Many thanks!

 Accepted Answer

The table appears to work stepwise. The interp1 function can interpolate using several options, here 'previous', 'nearest', and 'next' could apply. Choose the method that does what you want.
Example —
S235 = [16 235
40 225
63 215
80 215];
fy_fcn = @(Thickness) interp1(S235(:,1), S235(:,2), Thickness, 'nearest');
Thickness = 25;
fy = fy_fcn(Thickness)
producing:
fy =
235
.

4 Comments

Omar Aziz
Omar Aziz on 15 Nov 2020
Edited: Omar Aziz on 15 Nov 2020
that is great, thank you for the answer!
what does the @(Thickness) do exactly?
As always, my pleasure!
I created ‘fy_fcn’ as an anonymous function. It works just as any other function, and can be incorporated into a script (as I did here) without it having to be defined at the end of the script or saved as a separate function file. Anonymous functions are extremely useful in a variety of situations. They are explained in Anonymous Functions. The ‘@’ operator defines a function handle. See What Is a Function Handle? for more information on them.
Thanks for the explanation!
I have a follow up question.
Prior to this, I ask for a prompt so the user can specifiy the steel grade.
I am having trouble combing the two parts of the script - any ideas?
It seems if I offer non numeric choices ('S235' etc) then substituting S235 (from the answer above) for steel_grade I got an error.
steel_grade = questdlg('What is the steel grade (S)', mfilename, 235, 275, 355,'quit');
As always, my pleasure!
I would do something like this:
S235 = [16 235
40 225
63 215
80 215];
S275 = [16 275
40 265
63 255
80 245];
S355 = [16 355
40 345
63 335
80 325];
steel_grade = questdlg('What is the steel grade (S)', mfilename, 235, 275, 355,'quit');
thicknsc = inputdlg('Thickness:', 'What is the thickness?');
thickns = sscanf(thicknsc{:},'%f');
switch steel_grade
case 235
fy = interp1(S235(:,1), S235(:,2), thickns, 'nearest')
case 275
fy = interp1(S275(:,1), S275(:,2), thickns, 'nearest')
case 355
fy = interp1(S355(:,1), S355(:,2), thickns, 'nearest')
end
I tested it, and it works for all of them. Tha anonymous function is not necessary here, since in this context it simply adds another layer of complexity.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation 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!