How to set a dynamic property as transient?

10 次查看(过去 30 天)
Consider the following example which defines a class that contains a dynamic property 'bigData'. When the class gets saved, 'bigData' gets saved along with the other properties of the class. Later, the "Transient" attribute of the dynamic property is set true. In the subsequent save operation, 'bigData' should not be saved with the other class properties, however, the size of the resulting file is the same for Transient=true as it is for Transient=false. The Transient attribute does not appear to have any affect on the size of the saved file. What is needed to get the Transient attribute to take effect?
classdef (ConstructOnLoad = true) TestDynProps < dynamicprops
properties
a;
dp;
end
methods
function this = TestDynProps()
this.a = [];
this.dp = addprop(this, 'bigData');
end
function testSave(this)
this.a = [2,4,6,8];
this.bigData = rand([1000, 1000]);
save("transient_false.mat", 'this')
this.dp.Transient = true;
save("transient_true.mat", 'this')
end
end
end
>> tdp = TestDynProps()
tdp =
TestDynProps with properties:
a: []
dp: [1×1 meta.DynamicProperty]
bigData: []
>> tdp.testSave()

回答(1 个)

Kautuk Raj
Kautuk Raj 2024-2-26
From what I gather, you are seeking clarification on why setting the Transient attribute to true for a dynamic property in MATLAB does not reduce the size of the saved file and what changes are necessary to ensure the attribute effectively excludes the property from being saved.
The Transient attribute for dynamic properties in MATLAB is designed to indicate whether a property should be saved when the object is saved to a file. If the Transient attribute is set to true, the property should not be saved. However, in the given example, setting the Transient attribute to true just before saving does not seem to exclude bigData from being saved.
To ensure that the Transient attribute takes effect, you should modify the class definition to allow the dynamic property to be configured properly before any save operation is attempted. Here is how you could modify the TestDynProps class to achieve this:
classdef (ConstructOnLoad = true) TestDynProps < dynamicprops
properties
a;
end
methods
function this = TestDynProps()
this.a = [];
this.addBigDataProperty(false); % initially not transient
end
function addBigDataProperty(this, isTransient)
if isprop(this, 'bigData')
delete(this.findprop('bigData'));
end
dp = this.addprop('bigData');
dp.Transient = isTransient;
end
function testSave(this)
this.a = [2,4,6,8];
this.bigData = rand([1000, 1000]);
save("transient_false.mat", 'this')
this.addBigDataProperty(true); % now make it transient
save("transient_true.mat", 'this')
end
end
end
The modified class uses the addBigDataProperty method to manage the bigData dynamic property's Transient attribute, ensuring it is set appropriately before each save operation within the testSave method.
More about the Transient property can be gathered from the MathWorks documentation page below: https://www.mathworks.com/help/releases/R2020b/matlab/matlab_oop/property-attributes.html#:~:text=and%20Query%20Events-,Transient,-logical

类别

Help CenterFile Exchange 中查找有关 Software Development Tools 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by