Conversion from an old to a new version of a class with different property names

조회 수: 35 (최근 30일)
Dani A
Dani A 2025년 8월 26일 19:55
편집: Dani A 2025년 8월 26일 20:26
Let's say that I have a custom class testClass
classdef testClass
properties
input1
computationResultPropertyNameThatsTooLong
end
methods
function tc = testClass(input1)
tc.input1 = input1;
expensiveFunction = @sqrt;
tc.computationResultPropertyNameThatsTooLong = expensiveFunction(input1);
end
end
end
I'm offloading the computation of the testClass objects (of which I have tens of thousands) to another machine. Is there any way to make it so that I can alter the property names of testClass in a "clean " way (let's say I change computationResultPropertyNameThatsTooLong to computationResultBetterName) such that I can load in the data from the *.mat file as valid testClass objects? I can of course run the computations again, but I would prefer not to.

채택된 답변

Matt J
Matt J 2025년 8월 26일 20:08
편집: Matt J 2025년 8월 26일 20:11
One way is with a Dependent property. This makes it so you can reference the same property using tc.shortname.
classdef testClass
properties
input1
end
properties (Dependent)
shortname
end
properties (SetAccess = immutable, Hidden)
computationResultPropertyNameThatsTooLong
end
methods
function tc = testClass(input1)
tc.input1 = input1;
tc.computationResultPropertyNameThatsTooLong = expensiveFunction(input1);
end
function val=get.shortname(tc)
val=tc.computationResultPropertyNameThatsTooLong;
end
end
end
  댓글 수: 1
Dani A
Dani A 2025년 8월 26일 20:25
편집: Dani A 2025년 8월 26일 20:26
wow ok so I think this works ! setting it as a dependent property and the old property as hidden and immutable is clever. I'm testing it with my real data - i'll accept your answer later this evening when I've confirmed that it works

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Class Introspection and Metadata에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by