Access property of class object found by "whos"

5 visualizaciones (últimos 30 días)
Pedro
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
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:
Pedro
Pedro el 9 de Dic. de 2023
Hello Stephen,
Thank you for your fast reply.
Using it as a function I could indeed find the object, but I get your point as I would not be able to access it anyway from inside the function because the object was in another workspace.
Now what I did was instead of running it as a function running it as a script (for my use cases this poses no problem at all):
obj = myClass(); % Create a dummy object of 'myClass'
s = whos; % Gather all variables in the workspace
% 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
Now I could in theory acces my object "obj", but the problem that remains is that I still do not know how to do it from the output of "whos".
Would you have any idea on how to counter this problem? Maybe "whos" in this case is not the correct function to use for what I want to do?
Thank you again,
Best regards,
Pedro

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
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.
  1 comentario
Pedro
Pedro el 9 de Dic. de 2023
Thank you Walter,
This is exactly what I was looking for.!
I will also take a look into your suggestion as it surely seems more robust.
Best regards,
Pedro

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
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
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by