MATLAB: How to find out if a graphics object property exists? ISPROP returns that property does not exist but using GET the value of property can be set.

getgraphicsispropMATLABobjectproperty

How to find out if a graphics object property such as "defaulterrorbarlinewidth" exists for the current figure?
Refer to the following example:
isprop(gcf,'defaulterrorbarlinewidth')
ans =
logical
0
get(gcf, 'defaulterrorbarlinewidth')
ans =
0.5
Even though the function "isprop" returns that "defaulterrorbarlinewidth" is not a property of the current figure "gcf", using the "get" function the value of it can be set?

Best Answer

In the above example searching for the property "defaulterrorbarlinewidth" returns "false" but you can set the value of the property. This happens because while "defaulterrorbarlinewidth" is not a property of the current figure handle, it is a factory property of the graphics root object "groot". The way MATLAB finds default values is listed in the following link:
MATLAB searches for a default value beginning with the current object and continuing through the object's ancestors until it finds a user-defined default value or until it reaches the factory-defined value. Therefore, a search for property values is always satisfied when using "get" or "set" methods with a particular "propertyName". In the example above, once the command
get(gcf, 'defaulterrorbarlinewidth')
is executed, the search ends when MATLAB finds the factory property "factoryErrorbarLineWidth" of the "groot" or the root graphics object and returns the value of that property. So, a possible workaround to handle such cases is to execute the following commands:
1. Use the function "get" to access all properties of the current figure by executing the command:
 
prop = get(groot, 'factory'); % get returns a struct with all the properties of groot
2. Search for the desired property "propertyName" throughout the field-names of the structure returned by "get"
 
propExists = any(strcmp('propertyName', fieldnames(props))); % this will return a boolean result : "true" if the property exists; otherwise "false"
The function "fieldnames" extracts the names of all the fields in the struct "props". These are the names of the properties that exist for the figure.
The function "strcmp" checks if any of the property names matches the one you are searching for. 
The function "any" returns "logical 1" or "true" if any of the elements passed to the function is "1". 
Therefore, "propExists" will only return "true" if the desired property exists for the figure.
Executing the above commands for the example in the question produces the following results:
 
prop = get(groot, 'factory');
any(strcmpi('factoryerrorbarlinewidth', fieldnames(prop)))
ans =
logical
1
Note that you have to use the actual property names "factoryerrorbarlinewidth" to get a match. Using "defaulterrorbarlinewidth" will not work:
any(strcmpi('defaulterrorbarlinewidth', fieldnames(prop)))
ans =
logical
0