Declare variables from Matlab function workspace to Simulink worskpace

5 ビュー (過去 30 日間)
Alberto Mora
Alberto Mora 2017 年 9 月 21 日
編集済み: Alberto Mora 2020 年 6 月 8 日
Hello,
I have a MATLAB script that call a Matlan function.
In this Matlab function, I compute some matrix and then I run a Simulink Simulation.
Obvioulsy, the variable computed inside the function are indipendent from the base workspace and for this reason Simulink model "don't see" these matrix.
How is possible to calculate the matrix inside this Matlab function and then "send" it into Simulink model?

採用された回答

Alberto Mora
Alberto Mora 2020 年 6 月 5 日
At the end I found the solution to "send" the variables from the function workspace to the simulink workspace.
ModelName='NameOfYourSimulinkModel';
GenericArray = [1 2 3]; % Same name must be used in Simulink model
hws = get_param(ModelName,'modelworkspace'); % Handle the model's workspace
hws.DataSource = 'MAT-File';
hws.FileName = 'workspace';
hws.assignin('GenericArray', GenericArray); % Send variable

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 9 月 21 日
The From Workspace block typically defaults to getting variables from the caller (that is, from the executing function), so just make sure your From Workspace block is not configured as extracting the data from the base workspace. You can configure To Workspace blocks to write to caller.

Community Treasure Hunt

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

Start Hunting!

Translated by