how to create a look up table to use in simulink from a 2D array of doubles

I have a simple 2 day array that is 40x2
I want to make it a look up table for simulink.
it needs to be interpolated not a direct.
So i want simulink to scan the first column and find the value closed to the one im inputting and them pump out the value in the second column that corresponds to the input found in column one.
This should be really simple. Can someone lend a hand?
thanks

 Accepted Answer

Assuming your 40x2 array is stored in a workspace varaiable named data.
Set the Table Data parameter to data(:,2)
Set the Breakpoints parameter to data(:,1)
Set the Interpolation Method to Nearest.
Depending on the structure of data(:,1) make sure to check into the following block parameters: Index search method, and Begin index search using previous index result
I think this addresses the problem as stated in the question. But the question also mentioned interpolation, and there is no interpolation happening here.

8 Comments

Thanks
I tried that, the numbers are not even coming close i dont get it.
This is really getting rediculous. This should be so simple and intuitive.
Its anything but.
There is no logical reason why the values should be grossly off
The input and output of the table are all linear and monotomically increasing.
The fact that the output is off by more then 50% is absurd
column1 = linspace(0,5,40)'
column2 = linspace(0,1,40)'
lut = column1;
lut(:,2) = column2
% and when i input like 4.5 into the Lut i should be around .9 i get .5
% instead.
I am at a total loss with this thing
I have tried just about every button possible.
The table data is set to the column 1
the breakpoint 1 is set to column2(1,1)
Well, you're not showing any results nor showing anything in your model, so it's difficult to help. Why not post your data using the paper clip icon on the Insert menu and some images of you Look Up Table block parameters using the image icon (left of the paper clip) on the Insert menu?
Edit: I must have been typing this comment while you were typing the preceding comment with the column1 and column2 data
This is not a direct lookup
This is an interpolation table.
The column 1 only has certain values in it.
When the input does not equal an exact number in the input, it should find the closest thing to it in the column1 and then output the column 2 value as the output.
I guess I don't understand what is meant by "direct lookup" and "interpolation table." Suppose we have
data = [1 4 10;10 12 20].'
data = 3×2
1 10 4 12 10 20
Is this the output you want for these inputs
in = [.5 , 3.1 , 6 , 10.5];
out = interp1(data(:,1),data(:,2),in,'nearest','extrap')
out = 1×4
10 12 12 20
Here's the equivalent Simulink model
I used column1 and column2 in the model immmediately above.
As you've described the problem:
Table data should be column2
Breakpoints 1 should be column1.
I get 0.8974 as the output, as expected:
column1 = linspace(0,5,40)';
column2 = linspace(0,1,40)';
interp1(column1,column2,4.5,'nearest')
ans = 0.8974
Paul you could show the picture of Table and breakpoints field so that it is clear for OP how to define them in Simulink.
Wow im an idiot.
I figured it out.
I guess i did not understand what the meaning of break point is.
Why dont they just call it data in and data out..... (headbang!)
Thank you guys for help so much! Great support great community. Im just a goober.
Happy Holidays folks

Sign in to comment.

More Answers (2)

To create a Simulink 1D Lookup Table block using the data from your 2D matrix variable,
modify this script to meet your needs and run it.
% fake creation of data
%
npts = 5;
x = linspace(0.1,1,npts).';
y = exp(1.234*x);
data2D = [x, y];
% Create model containing 1D LUT block
%
rand_trailing_string = dec2hex( randi([0,2^32-1],1,1), 8 );
mdl = sprintf('myRandModelName_%s',rand_trailing_string);
sourceLUT1 = sprintf('simulink/Lookup\nTables/1-D Lookup\nTable');
destinationLUT1 = [mdl,'/LUT'];
new_system(mdl);
open_system(mdl);
add_block( sourceLUT1, destinationLUT1)
dataConnectionApproach = 'inline';
switch dataConnectionApproach
case 'inline'
% explicitly inline the data values
% Hint: mat2str is super helpful here
%
stringForBreakpoints = mat2str(data2D(:,1),17,'class');
stringForTableData = mat2str(data2D(:,2),17,'class');
otherwise
% Reference data in workspace by variable name
% Note: data must always be defined before model is used
% in the future
%
stringForBreakpoints = 'data2D(:,1)';
stringForTableData ='data2D(:,2)';
end
set_param(destinationLUT1, 'BreakpointsForDimension1', stringForBreakpoints);
set_param(destinationLUT1, 'Table', stringForTableData );
% Make additional changes to LUT as desired
% Either
% Manually
% or
% use set_param
%
% Hint to see names of parameters to use with set_param do
%
% get_param(gcb,'DialogParameters')
%
open_system(destinationLUT1) % open for manual changes

1 Comment

This is not working.
I got the table working but the output valuesa are no where near where they should be.
It looks up a value of lets say 4, the corresponding value in the table is about .85
Instead the table pumps out .5
So i dont know how its doing this interpolation but its pretty far off

Sign in to comment.

You guys are putting in great effort here to help.
Thank you!
For the life of me i cant make this work. I got rid of all the complicated code as you did and it still wont work. Here is a screen shot

1 Comment

Wow im an idiot.
I figured it out.
I guess i did not understand what the meaning of break point is.
Why dont they just call it data in and data out..... (headbang!)
Thank you guys for help so much! Great support great community. Im just a goober.
Happy Holidays folks

Sign in to comment.

Categories

Find more on Aerospace Applications in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 13 Dec 2023

Commented:

on 15 Dec 2023

Community Treasure Hunt

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

Start Hunting!