Variable Window Not Updating Automatically when code is executed

21 vues (au cours des 30 derniers jours)
Cody Brown
Cody Brown le 22 Août 2024
Modifié(e) : Cody Brown le 23 Août 2024
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.

Réponses (1)

Subhajyoti
Subhajyoti le 23 Août 2024
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 commentaire
Cody Brown
Cody Brown le 23 Août 2024
Modifié(e) : Cody Brown le 23 Août 2024
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Enumerations dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by