Why the user-defined function return an empty matrix when there is a division formula about global variables?
조회 수: 1 (최근 30일)
이전 댓글 표시
Here is my code:
function ans = rho_free(xm)
global m ;%Where m = 1 is defined in the first few lines
ans = xm./m;
end
I've put this part of code at the end of the document, and defined
m = 1;
at the very begining of the same document.
When I call the function rho_free(2), I get an empty matrix. And I don't know why and have no idea how to solve this problem.
Thank
댓글 수: 0
채택된 답변
Mehmed Saad
2020년 5월 1일
You need to define
global m
before calling the function rho_free. if global m is not defined m will be empty
the best way is
global m
m = 1
rho_free(2)
ans =
2
On the other hand if (clear all) is applied at the start of the code, global variable will be cleared (and last value is not retained)
Now suppose i directly call rho_free without initializing global m ( considering it has no value before) then
rho_free(2)
ans =
[]
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!