필터 지우기
필터 지우기

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 CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by