MATLAB: Does using ‘copyobj’ function with ‘uitreenode’ break node selection

MATLABselectionuitreeuitreenode

I am using 'copyobj' function to copy 'uitreenode' within the same 'uitree'. The interactive node selection within a 'uitree' misbehaves when a 'uitreenode' is copied within its parent 'uitree'. When a node is created using 'copyobj' function, the new node and the node it was created from can both be selected, even if the 'Multiselect' property was turned off.
The behavior also persists if a node is copied to a separate 'uitree' and then the new object's parent set to the original.

Best Answer

This is a known bug which has been resolved in MATLAB R2018b.
If you are experiencing this issue in MATLAB R2018a or an earlier release, you can use one of the following workarounds:
1. The recommended workaround is to create a custom function to copy 'uitreenodes' as shown below:
function nodeCopy = treeNodeCopyObj(node, parent)
nodeProperties = get(node);
nodeProperties = rmfield(nodeProperties, 'BeingDeleted');
nodeProperties = rmfield(nodeProperties, 'Type');
nodeCopy = uitreenode(parent, nodeProperties);
end
2. Another potential workaround, which is less recommended, is to create a custom 'copyobj' function and put it in your path above the built-in version as shown below:
function nodeCopy = copyobj(node, parent)
if isa(node, 'matlab.ui.container.TreeNode')
nodeProperties = get(node);
nodeProperties = rmfield(nodeProperties, 'BeingDeleted');
nodeProperties = rmfield(nodeProperties, 'Type');
nodeCopy = uitreenode(parent, nodeProperties);
else
builtin('copyobj', node, parent)
end