- Undocumented Matlab - https://undocumentedmatlab.com -

copyobj behavior change in HG2

Posted By Yair Altman On May 13, 2015 | No Comments

As a followup to last-week’s post on class-object and generic data copies, I would like to welcome back guest blogger Robert Cumming, who developed a commercial Matlab GUI framework [1]. Today, Robert will highlight a behavior change of Matlab’s copyobj function in HG2.
One of the latest features that was introduced to the GUI Toolbox was the ability to undock or copy panels [2], that would be displayed in a standalone figure window, but remain connected to the underlying class object:

Panel copy in the GUI framework toolbox [2]
Panel copy in the GUI framework toolbox

These panel copies had to remain fully functional, including all children and callbacks, and they needed to retain all connections back to the source data. In the example above I have altered the plot to show that it’s an actual copy of the data, but has separate behavior from the original panel.
To simply undock a uipanel to a new figure, we can simply re parent it by updating its Parent property to the new figure handle. To make a copy we need to utilize the copyobj function, rather than re-parenting. copyobj can be used to make a copy of all graphic objects that are “grouped” under a common parent, placing their copy in a new parent. In HG2 (R2014b onwards) the default operation of copyobj has changed.

When I started developing this feature everything looked okay and all the objects appeared copied. However, none of the callbacks were functional and all the information stored in the object’s ApplicationData was missing.
I had used copyobj in the past, so I knew that it originally worked ok, so I investigated what was happening. Matlab’s documentation for HG2 code transition [3] suggests re-running the original code to create the second object to populate the callbacks. Unfortunately, this may not be suitable in all cases. Certainly in this case it would be much harder to do, than if the original callbacks had been copied directly. Another suggestion is to use the new ‘lagacy’ option’:

copyobj(___,’legacy’) copies object callback properties and object application data. This behavior is consistent with versions of copyobj before MATLAB® release R2014b.

So, instead of re-running the original code to create the second object to populate the callbacks, we can simply use the new ‘legacy’ option to copy all the callbacks and ApplicationData:

copyobj(hPanel, hNewParent, 'legacy')

Note: for some reason, this new ‘legacy’ option is mentioned in both the doc page and the above-mentioned HG2 code-transition page, but not in the often used help section (help copyobj). There is also no link to the relevant HG2 code-transition page in either the help section or the doc page. I find it unfortunate that for such a backward-incompatible behavior change, MathWorks has not seen fit to document the information more prominently.
Other things to note (this is probably not an exhaustive list…) when you are using copyobj:

  • Any event listeners won’t be copied
  • Any uicontextmenus will not be copied – it will in fact behave strangely due to the fact that it will have the uicontextmenu – but the parent is the original figure – and when you right-click on the object it will change the figure focus. For example:
    hFig= figure;
    ax = axes;
    uic = uicontextmenu ('parent', hFig);
    uim = uimenu('label','My Label', 'parent',uic);
    ax.UIContextMenu = uic;
    copyChildren = copyobj (ax, hFig, 'legacy');
    hFig2 = figure;
    copyChildren.Parent = hFig2;
    

Another note on undocked copies – you will need to manage your callbacks appropriately so that the callbacks manage whether they are being run by the original figure or in a new undocked figure.

Conclusions

  1. copyobj has changed in HG2 – but the “legacy” switch allows you to use it as before.
  2. It is unfortunate that backward compatibility was not fully preserved (nor documented enough) in HG2, but at least we have an escape hatch in this case.
  3. Take care with the legacy option as you may need to alter uicontextmenus and re-attach listeners as required.

Categories: Guest bloggers, GUI, Handle graphics, Low risk of breaking in future versions, Stock Matlab function


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/copyobj-behavior-change-in-hg2

URLs in this post:

[1] Matlab GUI framework: https://sites.google.com/site/matpihome/matlab-gui-framework

[2] undock or copy panels: https://sites.google.com/site/matpihome/matlab-gui-framework/matlab-undock-uipanel

[3] Matlab’s documentation for HG2 code transition: http://mathworks.com/help/matlab/graphics_transition/why-are-callbacks-not-copied.html

[4] Property value change listeners : https://undocumentedmatlab.com/articles/property-value-change-listeners

[5] Handle Graphics Behavior : https://undocumentedmatlab.com/articles/handle-graphics-behavior

[6] Undocumented scatter plot behavior : https://undocumentedmatlab.com/articles/undocumented-scatter-plot-behavior

[7] xlsread functionality change in R2012a : https://undocumentedmatlab.com/articles/xlsread-functionality-change-in-r2012a

[8] uicontextmenu performance : https://undocumentedmatlab.com/articles/uicontextmenu-performance

[9] Performance: accessing handle properties : https://undocumentedmatlab.com/articles/performance-accessing-handle-properties

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.