Why do global variables not update in the Workspace Browser or the Array Editor when I modify them from within a function?
조회 수: 2 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2009년 6월 27일
댓글: Nalini Vishnoi
2015년 7월 13일
I declare a global variable "A" in the base MATLAB workspace and assign it a value as follows:
global A
A=1;
The workspace browser shows "A" is equal to 1. I open the variable "A" in the array editor and see that the correct value is displayed here as well. I assign a new value to "A" from the command line as follows:
A=2;
I see that both the workspace browser and the array editor show the correct value for "A". Now I modify "A" using an M -file function as follows:
function seta(x)
global A
A=x;
When I modify "A" using the function "seta", the Workspace Browser and Array Editor do not get updated with the correct value of "A". When I just type "A" at the Command Line the Array Editor updates, but the Workspace Browser does not. I need to right click in the Workspace Browser and select Refresh from the Context Menu to update the value of "A". If I assign a new value to "A" from the Command line as follows:
A=5;
both the workspace browser and the array editor will update correctly regardless of any updates I have made using "seta".
채택된 답변
MathWorks Support Team
2009년 6월 27일
This bug has been fixed in Release 14 Service Pack 2 (R14SP2). For previous releases, please read below for any possible workarounds:
We have verified that there is a bug in MATLAB 7.0.1 (R14SP1) that affects the way the values of global variables are updated in the Workspace Browser and the Array Editor when they are modified from within a function. The values of global variables update correctly when the variables are modified in the base MATLAB workspace, i.e., from the command line. This issue has been forwarded to our development team for consideration for a future release of MATLAB.
To work around this problem, use EVALIN as follows:
function seta(x)
global A
A=x;
evalin('base','A=A;');
This forces the update by executing the assignment statement, "A=A;", in the base MATLAB workspace.
댓글 수: 0
추가 답변 (1개)
timo
2015년 4월 25일
I think I am getting this bug in 2015a again lol :(
댓글 수: 1
Nalini Vishnoi
2015년 7월 13일
Hi Timo,
I was not able to reproduce the same behavior in MATLAB R2015a. I would recommend to contact MathWorks Technical Support if you continue to see this issue. I hope this helps.
Nalini
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!