How do I declare a variable in the MATLAB workspace from a MATLAB function?
9 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2009 年 6 月 27 日
編集済み: MathWorks Support Team
2021 年 12 月 13 日
I wrote a function that provides some variables to Simulink. I have declared them global, so Simulink could see them. But I have to declare them at the prompt and I am unable to declare them in the function because Simulink does not see them.
採用された回答
MathWorks Support Team
2021 年 12 月 10 日
編集済み: MathWorks Support Team
2021 年 12 月 13 日
Simulink has access to any variable defined in the MATLAB workspace. You do not need to declare a variable 'global'. In order to define a variable in the MATLAB workspace from a function, you can declare the variables in the function using the command EVALIN. For example:
function declare_variable
......
evalin('base','k=100');
......
In this example, when the function 'declare_variable' is called, it will generate a variable 'k' in the MATLAB workspace. This variable can then be seen by the Simulink model. Please note that if, before the function 'declare_variable' is called, a variable called 'k' exists in the MATLAB workspace already, calling the function 'declare_variable' will overwrite the existing variable 'k' with the new value (100).
For more information on the EVALIN function, see the following URL:
0 件のコメント
その他の回答 (1 件)
Gaganjyoti Baishya
2020 年 6 月 20 日
Hi,
You can easily add variables in base from function using assignin.
function myFunc(a,b)
x=3;
assignin('base','myVar',x);
end
assignin declares the varibale myVar with value of 3.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Simulink Functions についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!