Variable Window Not Updating Automatically when code is executed

25 次查看(过去 30 天)
When I run some code which changes the contents of a class in my workspace, that change isn't showing in the variables window unless I close this property and re-open it in the variables window. As you can see, when I quirry what this value is in the command prompt, I see that it doesn't match the varibles window.
How is the "variables window" getting updated? Are there some settings in Matlab to adjust if the variables window automatically updates itselft on a perioidic basis, like once every 500mS or something? I don't recall ever experiencing this before.
Please note that this array that I am looking at in the screenshot below, is an array of an enumeration class, where 'SK1' in the command window corelates to 1.0000 in the variables window.

回答(1 个)

Subhajyoti
Subhajyoti 2024-8-23
The MATLAB "Variables" window updates its contents whenever a script or function has finished executing, rather than on a periodic basis during execution. This means that the variable values are refreshed once the script completes, rather than continuously every few milliseconds. Here's a GIF demonstrating the expected behaviour:
Below is the sample class and test functions I used to explore this:
‘sampleClass.m’:
classdef sampleClass
properties (SetAccess = private)
prop1 = 0;
prop2 = 0;
end
methods (Access = public)
function obj = setProps(obj, val)
obj.prop1 = val*2;
obj.prop2 = val*val;
end
function getProps(obj)
fprintf('prop1: %d\n', obj.prop1);
fprintf('prop2: %d\n', obj.prop2);
end
end
end
‘testFunction.m’:
obj = sampleClass;
obj = testFunction(obj, 3);
pause(0.000001)
obj = testFunction(obj, 5);
pause(0.001);
obj = testFunction(obj, 11);
pause(1);
function obj = testFunction(obj, value)
% Set the properties
obj = obj.setProps(value);
% Get the properties
obj.getProps();
end
This test function demonstrates how the "Variables" window updates after the script execution.
You may go through the following MathWorks documentation links to learn more about ‘Workspace and Variable Preferences’:
  1 个评论
Cody Brown
Cody Brown 2024-8-23
编辑:Cody Brown 2024-8-23
You are saying that the variables window updates upon completion of a script or function, which would be okay. However, what I am showing is that I am calling a method in a class (like a function), and after calling that method which changes some values, those values aren't updated in the variables window.
Here is some example code I put together which shows this happening. It looks like there is an issue when updating enumerations, and those enumerations not updating in the variables window.
classdef TestClass < handle
%TESTCLASS Summary of this class goes here
properties
Gyros (:,1) GyroModel
end
methods
function obj = TestClass()
%TESTCLASS Construct an instance of this class
obj.Gyros = [GyroModel(1); GyroModel(2); GyroModel(3)];
end
function method1(obj)
%METHOD1 Summary of this method goes here
obj.Gyros = circshift(obj.Gyros(:,1),1);
end
end
end
classdef GyroModel < double
%GYROMODEL Summary of this class goes here
% Detailed explanation goes here
enumeration
NA (0)
SK1 (1)
SK2 (2)
SK3 (3)
SK4 (4)
end
end
test = TestClass();
%open, in the variables window, the contents for test.Gyros
%test.Gyros should = [1;2;3]
test.method1();
%test.Gyros should = [3;1;2]
%note that the variables window (still open) still shows test.Gyros =
%[1;2;3] which is wrong!
test.Gyros
%when you return the contents of test.Gyros you will get the corret values
Note, that if you close and re-open the 'test.Gyros' tab in the variables window then they it will show the updated values.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Enumerations 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by