필터 지우기
필터 지우기

The global variable is lost between workspaces

조회 수: 1 (최근 30일)
H R
H R 2016년 6월 26일
답변: Walter Roberson 2016년 6월 26일
I have written a matlab file (base.m) for an optimization problem that uses the genetic algorithm also in which I declare a global variable A. I am using x = ga(@f(x),2,[],[],[],[],LB,UB). Note that, I have also a function f that is defined in a file called function.m. In the function I am using the global variable A which has also been declared in base.m.
However, when ga is performed to call function f, the global variable A is lost in the function evaluation.
How can I fix this problem?
  댓글 수: 1
Image Analyst
Image Analyst 2016년 6월 26일
So you're saying that when "f" gets called and it comes time to use A, it throws an error saying that there is no such variable? We'd need to see how you defined f. I want to make sure that you actually defined A as global in "f". Please post a simplified example that demonstrates the problem.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2016년 6월 26일
Global variables need to be defined as global in every function that they are used.
Do you truly need a global variable? Or do you just need to parameterize your function ?

카테고리

Help CenterFile Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by