letting an object name itself
11 views (last 30 days)
Show older comments
Nathan Bblanc
on 15 Sep 2020
Commented: Nathan Bblanc
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
Stephen23
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.
Accepted Answer
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
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!