Clear Filters
Clear Filters

Variable Window Not Updating Automatically when code is executed

46 views (last 30 days)
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.

Answers (1)

Subhajyoti
Subhajyoti on 23 Aug 2024 at 12:46
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 Comment
Cody Brown
Cody Brown on 23 Aug 2024 at 16:04
Edited: Cody Brown on 23 Aug 2024 at 16:13
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.

Sign in to comment.

Categories

Find more on Enumerations in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!