How to set a dynamic property as transient?
11 ビュー (過去 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()
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/503323/image.jpeg)
0 件のコメント
回答 (1 件)
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Time Series についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!