MATLAB: Importing a matrix in Simulink, extracting rows and interpolate

data importinterpolation

Hello,
I have spent a good amount of hours searching for the answer to my problem on this website, I hope this post doesn't replicate another one.
I have a matlab function which loads a data file, searches the lines of interest in the matrix depending on inputs and interpolates using interp1. It works very well. However, when I input it in a Matlab function bloc in Simulink, it doesn't work. First error is the "importdata" which doesn't work. Then, the sign "==" for dynamic calculation like in: table = table( table(:,1) == MN , :); (where MN is an input) Eventually the "interp1" function doesn't seem to work ( underlined in red but since the function freezes before I can't say).
Any good way to solve this problem?
Also, I have tried the "from workspace" but then I don't care about the time parameter because I just want my matrix (it is absolutely not time dependent) and I want to play on its values.
Thanks

Best Answer

Here's what I'd do :
change your function as :
function a = interpolation(MyInput)
% 1 - remove the line table = importdata('data.dat');
% 2 - fusion your input in one with a mux block in the simulink model
% get your 2 input
b = MyInput(1);
c = MyInput(2);
% get your matrix from the base workspace
table = evalin('base','table');
table = table(table(:,1) == b, :); % I just keep the data in table which has b in its first column
x = table(:, 2)';
y = table(:, 3)';
a = interp1(x, y, c);
end
then use this function in an interpreted matlab function (Not the embedded).
before the simulation call your import script
table = importdata('data.dat');
table = table.data;
so in the base workspace, table will be available (as a matrix).
and so normally, it should work.