Access property of class object found by "whos"
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Pedro
el 9 de Dic. de 2023
Comentada: Walter Roberson
el 9 de Dic. de 2023
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 comentarios
Stephen23
el 9 de Dic. de 2023
"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:
Respuesta aceptada
Walter Roberson
el 9 de Dic. de 2023
Editada: Walter Roberson
el 9 de Dic. de 2023
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.
Más respuestas (1)
Steven Lord
el 9 de Dic. de 2023
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 comentario
Walter Roberson
el 9 de Dic. de 2023
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.
Ver también
Categorías
Más información sobre Data Type Identification en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!