Access property of class object found by "whos"
7 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I use a user-defined class that stores data and I record the last updated time of this class in a property "LastUpdate".
I am writing a function to warn the user, when using an object of this class in a script, if the "LastUpdate" value is not the current date.
I thought about doing this by using the "whos" function to find any object of this class, then acessing the object and the property.
The problem I face is that I don't know how to access the object from it's name.
Here is what I wrote so far:
obj = myClass(); % Create a dummy object of 'myClass'
s = whos; % Gather all variables in the workspace
checkLastClassUpdateTime(s);
%
function [] = checkLastClassUpdateTime(s)
% This function performs a sanity check to see if the 'myClass' object
% has outdated data, and warns the user if so
% Source: https://www.mathworks.com/matlabcentral/answers/95568-how-do-i-find-all-the-variables-of-a-given-class-in-the-matlab-workspace
% find the objects with the type 'ClassName' from the workspace:
matches = strcmp({s.class}, 'myClass');
my_variables = {s(matches).name};
%
if isempty(my_variables)
warning("No variables of type 'myClass' found in present workspace");
else
lastUpdate = (s(matches).name).LastUpdate; % Here is the problem: s(matches).name gives me "obj", but how can I actually **access** 'obj'? The current line gives me an error
if lastUpdate < datetime("today")
warning("Please update the class");
end
end
end
I hope the question and example are clear. If not, I would be glad to share more precise insights.
Thank you!
Best regards,
Pedro
2 件のコメント
Stephen23
2023 年 12 月 9 日
"I thought about doing this by using the "whos" function to find any object of this class..."
That won't find any object of that class, at best it will only find them in one specific workspace:
採用された回答
Walter Roberson
2023 年 12 月 9 日
編集済み: Walter Roberson
2023 年 12 月 9 日
What you are looking for is
VALUE = evalin(caller, s(matches).name + ".LastUpdate")
We do not recommend this, however.
If you can provide a template for the class, then make it a handle class and give the class a static property that is a list of values, and have the constructor "register" the newly-created object in the list of values, and have the class destructor de-register from that list. With this configuration, the class has a list of all members of the class and so can check them all to see if they are current enough.
Static members of a class are stored against the class definition, and are accessible to all members of the class.
その他の回答 (1 件)
Steven Lord
2023 年 12 月 9 日
So when you use an instance of this class, you want that instance to check if it was last updated too long ago? Why not just store that information in the class instance itself as a property (probably with private SetAccess, so no one outside the class can manipulate that property) and have the class methods check the last updated date and time then update the property with the current date and time?
1 件のコメント
Walter Roberson
2023 年 12 月 9 日
Imagine if users were working with estimating costs to supply something that took multiple assemblies together. Costing out each assembly would require checking the inventory of parts and the current-prices list; you could store the result of a query in a .mat file, but after a day or so it would become potentially inaccurate and the query would need to be run again.
The final assembled assembly quote would put together the several different sub-quotes -- but it also needs to check that those sub-quotes are up-to-date enough, requring them to be regenerated if they are not.
The information about the update time is already being store against the class instance, according to what was posted.
The problem is to locate all of the class instances and verify that they are sufficiently up to date before proceeding with whatever the next step is.
参考
カテゴリ
Help Center および File Exchange で Data Type Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!