MATLAB Answers

letting an object name itself

1 view (last 30 days)
nathan blanc
nathan blanc on 15 Sep 2020
Commented: nathan blanc on 16 Sep 2020
i am working on an object oriented code. i would like every object to have a property 'name' that is identical to it's workspace name. (i didn't find any way to reference the workspace name in the code, but in any case i think this way is more convenient). the only way i found to do it is to have the user input the object's name as an input parameter in the constructor. e.g Nathan=Person('Nathan',property1,property2) this way is inconvenient and if the user gives a different name by mistake (e.g Nathan=Person('Batman',property1,property2)) it causes bugs. is there a better way to do this? or at least to generate an error messege if the names are different
Many thanks
Nathan

  3 Comments

Steven Lord
Steven Lord on 15 Sep 2020
What would you want the name property to be if the object were stored in an array or a cell array?
thePeople{42} = Person('Nathan', ...)
What would you want the name property to be if the object has no workspace name associated with it, not even a "container" in which it is stored?
steve.greet(Person('Nathan', ...)) % greet is a method of Person
How are you planning to use this information in the future? If you're planning to use it to try to manipulate the object using its name property that's generally discouraged.
Is your object a handle object? If so what should steve's name property be after executing the two lines of code below. steve and stefon refer to the same object, so is steve's name steve or stefon?
steve = Person('Steve', ...)
stefon = steve;
nathan blanc
nathan blanc on 15 Sep 2020
these are good questions that you are asking :)
my objects are all handle objects. the idea is simply for the workspace to be helpful for the user. to allow him/her to browse among the existing objects with ease.
e.g let's say Steve has a property Steve.friends - a cell array with handles to all of steve's friends. this is very nice but when you look at it you can't know which friend is which without entering the list and looking at them one by one. so i add another property Steve.friend_names - another cell array containing strings with the friend names. but this list is not helpful at all if the names are different from the workspace names, becuase once again you have to open the object to check what the actual names are. maybe the workspace is just not meant for serving such a purpose, and i should create my own inerface.
p.s.the first two situations you described will not happen in my code, and the third is really the user's problem. if they want to give two names to the same object then it's their obligation to keep track of it.
Stephen Cobeldick
Stephen Cobeldick on 16 Sep 2020
The difficulties arise due to the approach of forcing meta-data into the variable names,which invariably makes code slow, complex, and buggy.
The simple, efficient, robust approach would be to correctly store the meta-data (which after all is data) in a variable, not in the variable's name. And instead of having lots of separately named variables just use one simple object array to store them all in. Then your code will be much simpler and the data will be easier to process.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 16 Sep 2020
With the clarifications you gave I now have enough information to make some suggestions that may resolve the question, so I'm making this an answer. It's based on the second comment on the original question.
You've just described a scenario where the first situation does occur, assuming that friends are People.
" e.g let's say Steve has a property Steve.friends - a cell array with handles to all of steve's friends."
If you pass steve into a function (whose workspace does not contain the People objects in steve's friends property as independent objects) and that function assigns one of those friends to a temporary variable (theFriend = steve.friends{1};) does that change the name property of the variable in the calling workspace?
If you want to give People names and have a custom display method for People that shows their names (and even their friends' names) that seems reasonable to me. But if I were designing this interface I wouldn't tie those names to the implementation detail of which variable stores the object. As a very rough model:
classdef People < matlab.mixin.CustomDisplay & handle
properties
name
friends = {};
end
methods
function person = People(name)
person.name = string(name);
end
function addfriend(person, friend)
person.friends{end+1} = friend;
end
end
methods(Access = protected)
function propgrp = getPropertyGroups(person)
numFriends = numel(person.friends);
if numFriends == 0
friendNames = "None!";
else
friendNames = strings(numFriends, 1);
for whichFriend = 1:numFriends
friendNames(whichFriend) = ...
person.friends{whichFriend}.name;
end
friendNames = join(friendNames, ', ');
end
proplist = struct('Name', person.name, ...
'Friends', friendNames);
propgrp = matlab.mixin.util.PropertyGroup(proplist);
end
end
end
How to use this class?
% Make three People
steve = People('Steve') % steve's name is not steve but Steve.
bob = People('Bob') % ditto for bob and Bob
loren = People('Loren') % and loren and Loren.
% Make two friends
addfriend(steve, bob)
addfriend(steve, loren)
% Show the friendships
steve
% On a more formal occasion
steve.name = "Steven Lord" % Steven Lord is not a valid MATLAB variable name
% or
steve.name = "Lord, Steven" % comma may be optional ;) Also not a valid variable name

  1 Comment

nathan blanc
nathan blanc on 16 Sep 2020
thank you a lot for your help Steven. this is the third time you have solved my problem and i appreciate it very much. MATLAB is a truly amazing tool with elegant solutions to just about any problem.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!