Importing a matrix in Simulink, extracting rows and interpolate

조회 수: 3 (최근 30일)
Antoine
Antoine 2014년 11월 10일
편집: Orion 2014년 11월 10일
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

채택된 답변

Orion
Orion 2014년 11월 10일
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.
  댓글 수: 2
Antoine
Antoine 2014년 11월 10일
Orion, Thanks a lot, it works fine now! Now, this is very simple but I don't really know again how to do, the output is not only 1 number but 3 like [a1, a2, a3]. Do I have to keep a in the name of the function, and write a = [a1, a2, a3] before the end? Then, I don't understand because when I try to use a demux at the output to get a1, a2, a3 separately it doesn't work. Anyways, thanks again a lot!
Orion
Orion 2014년 11월 10일
편집: Orion 2014년 11월 10일
If you want multiple outputs (of the same data type of course), just make a vector in your function :
function MyOutput = 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)';
% creating a vector : here a 3x1 vector
MyOutput(1) = interp1(x, y, c);
MyOutput(2) = interp1(x, y, 2*c);
MyOutput(3) = interp1(x, y, 3*c);
end
Now, you get a vector as output of your matlab function block. use a demux block (of size 3) to get the individual signals.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Orion
Orion 2014년 11월 10일
Hi,
a lot of classical matlab functions are NOT supported by the "Matlab function" block (in the preivous version, it was "Embedded Matlab function")
you could instead use the "interpreted Matlab function" block, which call an external script .m (difference with the matlab function, which is a simulink block, so the code inside is interpreted by simulink not matlab).
BUT, it's not obvious, because all your Mcode will be executed at each time step. so you can encapsulate your Interpreted Matlab function in a enabled subsystem which is activate only at t==0, and then the output will be constant.
it will work but it's not really pretty. also, you can't generate C code with an interpreted function in a model.
Note : if your M-code calculate just a matrix independant of the time, why don't you load it in your model with a Constant block ?
  댓글 수: 1
Antoine
Antoine 2014년 11월 10일
Thanks Orion for your promptness.
Your note makes me believe that my explanation isn't clear enough. I'll try to be clearer:
I have a function called interpolation.m:
function a = interpolation(b,c)
table = importdata('data.dat');
table = table.data;
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
That code works fine in Matlab. Then, in Simulink, I want a bloc with b and c as inputs, a as output. Therefore I tried to use the "Matlab Function" (==embedded) and I had all these errors. So, to answer your question, b and c depend on time so the lines I select in the table are different at each time, but the data.dat is fixed.
Do you have any idea for this particular problem?
Thanks for your help.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Signal Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by