How to load pre-calculated matrix value in a file into Simulink for simulation ?

7 ビュー (過去 30 日間)
Hello,
We have a Matlab program to calculate a matrix and save it in a .mat file (using save(filename,'Matrix-Variable')). Now we want to use it in Simulink for simulation. What is the best way to do this ?
I am using the Matlab Function in Simulink and what to use load() command to load the matrix value. This works in Matlab workspace. But it seems dones't work for Simulink. Attached is the simple code. I always get 0 output (the real elment is another constant value) for y when I run the simulation.
Any suggestions and help will be highly appreciated. Thanks.
%This is a Matlab funciton block in Simulink. % % Try to read matrix R value from the file (fileR.mat) %
function y = fcn(init,u)
coder.extrinsic('load') coder.extrinsic('disp') filename = 'fileR'; %need to define the varible R (18x9) matrix R=zeros(18,9);
%The matrix R value is saved in the file already. Just want to load it load(filename, 'R');
%check the first element to see if the load is correct y = R(1,1); disp(y); end

採用された回答

Titus Edelhofer
Titus Edelhofer 2012 年 2 月 28 日
Hi Tom,
if it is a constant matrix I would do the following: add the load command to the preload function of the model (menu File->Model properties->Callbacks, write
load myfile.mat
and use the constant block with the name of the matrix in the file as parameter.
Titus
  1 件のコメント
Tom
Tom 2012 年 2 月 28 日
Thank you Titus. This is exactly what I am looking for. It works.

サインインしてコメントする。

その他の回答 (2 件)

Titus Edelhofer
Titus Edelhofer 2012 年 2 月 27 日
Hi Tom,
I would use the From File block to directly work with the matrix in the file. You would just need to make sure it has the right format (time and values, see the doc).
Titus

Tom
Tom 2012 年 2 月 27 日
Hi Titus,
Thank you for the quick reponse. I looked the From file block and was not sure what to do with the first rom, which should be time value. For my case, this is a constant matrix. Maybe I mis-understanded something there.
Thanks,
Tom

カテゴリ

Help Center および File ExchangeSimulink Environment Customization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by