フィルターのクリア

Accessing variables with functions in Matlab

3 ビュー (過去 30 日間)
Richard Miller
Richard Miller 2018 年 3 月 13 日
編集済み: Stephen23 2018 年 3 月 13 日
Suppose that I defined something in a .m file such as an initial velocity and I want to do a RK4 method to find numerical solutions for the velocity as time progresses. So, I define a function using the line: 'function RK4 = Runge_Kutta4()'.
In python, a function like this (e.g. RK4 = Runge_Kutta4()) would have access to the initial velocity, but could not change the variable. For my function to change the variable I would need to have the function call the variable using the line: 'RK4 = Runge_Kutta(Velocity)'. Is this true for Matlab as well?
  1 件のコメント
Stephen23
Stephen23 2018 年 3 月 13 日
編集済み: Stephen23 2018 年 3 月 13 日
" Is this true for Matlab as well?"
Forget everything you know about Python's scoping rules, they have no relevance to MATLAB at all. In terms of what the user sees, MATLAB is entirely pass by value: what happens in Vegas stays in Vegas!

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

採用された回答

James Tursa
James Tursa 2018 年 3 月 13 日
編集済み: James Tursa 2018 年 3 月 13 日
To have a function change a variable in MATLAB, one would typically do this:
x = myfunction(x);
And then the body of myfunction would change the value of x.
Another way is to use nested functions. E.g., if x is in a function and myfunction is a nested function within that first function, then myfunction could change x directly.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by