I am writing a method in a class which returns the name of a class object which was previously created using certain settings. So I need to know how do I extract only the name of the object created for those settings. Any help is appreciated!
MATLAB: How to get the name of the class object as a string
classclass objectmethodsstring
Related Solutions
Although beginners love making variables pop into existence in different workspaces, this is not robust programming, it is slow, and it removes lots of useful code checking tools. Yes, there are lots of code checking tools that simply do not work when you use eval, evalin or assignin. Click on a variable name in the editor, and it highlights all instances of that variable: does not work. Find and replace: does not work. Editor warnings: do not work. Because these variables just magically appear without warning, MATLAB cannot help you write your code, as it can when variables are passed and created correctly.
The typical functions used for this sloppy programming are eval, evalin, assignin. These are tools are the equivalent of a chainsaw, and should not be used where only a letter-opener is required. Their usage is a topic that has been covered many times before on this forum, and the usual consensus is "do not use these tools for basic assignment or moving variables around". How about these quotes from the MATLAB documentation "Share Data Between Workspaces":
" Best Practice: Passing Arguments The most secure way to extend the scope of a function variable is to use function input and output arguments"
And the least recommended method given on that page is:
"The evalin and assignin functions allow you to evaluate commands or variable names from strings and specify whether to use the current or base workspace. Like global variables, these functions carry risks of overwriting existing data. Use them sparingly."
Using them sparingly ideally means only using them in situations where none of the other methods will work. And, if a program is written well, this will be almost never...
To know more you can search this forum for "dynamic variable names" or "eval", and also read this page (while it is about eval, the same basic issues arise with evalin, etc):
And from MATLAB themselves:
Property access attributes cannot be modified at runtime in MATLAB.
However, there are ways to prevent the program from modifying the values of a property, once it has been "locked".
Approach 1:
Define the 'SetAccess' attribute for the property of class A that holds the object of class B as 'immutable'. Similarly define all properties of class B as 'immutable'. In this way, you can only set the values of the properties for the first time in the constructor.
Approach 2:
A more flexible approach is to define a flag property, and write your own code for 'set.propertyname' method in the class A. In this method, the property which contains the handle to object of class B, should be modified only if the flag is reset. Note that once the handle is available the user would still be able to read it, and modify the object of class B, but cannot modify the handle itself. As an example:
methods set.propertyname(object,value) % Write code for checking flag and assign value only if flag reset
endend
Further, to avoid modifying the object of class B, the value of flag would have to be queried from object of class A and implement a logic similar to the one above to make each property modifiable/not modifiable.
Best Answer