File Exchange

image thumbnail

copyUIAxes

version 3.0.0 (13.8 KB) by Adam Danz
Copies UIAxes (eg. AppDesigner) and most properties to a new figure and recreates the legend and colorbar if they exist.

62 Downloads

Updated 08 Jun 2020

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Use copyUIAxes() to copy plots in AppDesigner or any UIAxes onto regular axes which can be easily saved, printed, or modified.

copyUIAxes copies all children and most properties of a UIAxes to regular axes. If the UIAxes has a legend or colorbar, they can be recreated in the new axes, too.

copyUIAxes(ax) creates a new figure and copies the UIAxes (ax) content to the new figure.

copyUIAxes(ax, destination) copies the UIAxes to the destination which can be a figure or axis handle.

copyUIAxes(ax, 'copyPosition', true) will copy the position properties so the copied axes have the same position relative to the figure as the UIAxes or App.

See the file's help section for other optional inputs and several demos.

--------------------------------Notes--------------------------------
Matlab's copyobj() is not supported with UIAxes (eg. AppDesigner). In r2020a Matlab released exportgraphics() and copygraphics() which do support uiaxes and uifigures and makes parts of copyUIAxes() obsolete.

For discussion on this topic:
https://www.mathworks.com/matlabcentral/answers/281318-how-can-i-save-a-figure-within-app-designer

Cite As

Adam Danz (2020). copyUIAxes (https://www.mathworks.com/matlabcentral/fileexchange/73103-copyuiaxes), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (18)

Smile bag

Thank u,copytuiaxes() is perfect. When i use the copygraphics() to copy my uiaxes, but only copy to clipboard, not a new figure for modifying.

Adam Danz

@Lisa Månsson, I've been considering an option to copy axes positions and have already implemented it but I need more time for testing so check back for updates in a few days. Be sure to "follow" this page so you get can a notification when the function updates. In the meantime, here's a demo you can run on your machine and adapt to your needs. Replace the handles with your object handles. If you have further questions, please use the [Contact] button on my profile page to write to me rather than using this ratings section for discussion.

fh = uifigure();
uiax = uiaxes(fh);
C = imread('ngc6543a.jpg');
image(uiax,C)
axis(uiax,'tight')
hold(uiax,'on')
title(uiax,'sampleImage')

h = copyUIAxes(uiax);
h.axes.Units = uiax.Units
h.axes.Position = uiax.InnerPosition;

Hi!
Do you know of a simple way to make the pixel size of the figure the same as in the appGUI's axis from where I take it?
I tried different options but haven't found a solution yet. I did not get it working using exportgraphics in combo with tiledlayout. Using imresize I'm scared of modifying the image since there is interpolation involved and I don't think it should be nessecary when my original appGUI-figure is of the pixel size I want.

Apart from that, great function. Finally I get the background included when I save the figure. Thank you

Adam Danz

@Dovletgeldi Seyitliyev, thanks for the rating & feedback. There are several properties that are not copied from the UIAxes. Some of the properties such as the position properties are intentionally not copied because they would cause problems. Other properties are not copied because they are read-only. When calling copyUIAxes(), you can set 'listIgnoredProps' to true to print to the command window a table of graphics object properties that were not copied. The current version of copyUIAxes (vs 2.0) should have the same behavior as earlier versions. If you have found an exception to that or if you have a feature request, please PM me using the [Contact] button in my profile. I'd rather not turn this comment section into a discussion. Thanks again.

Thanks, it was handy to copy uifigure to normal figure and then "decorate" your figure for your needs there using plot tools, it was not just to copy figure,

Do you think there is workaround to achieve same thing that was working before ?

Adam Danz

@Dovletgeldi Seyitliyev, yes, the latest vs 2.0.0 is compatible with r2020a.

Does it work for 2020a

jonas

Thank you, excellent function.

Hope Q

I just used this function to copy an App Designer plot to a figure window so I could use the print statement to save it to a .png file. The function worked beautifully and was easy to add to my project. Many thanks to Adam.

Hello,
Cannot use this function, if XAxisTickLabels are cell array. Getting this error "Value must be a vector of type single or double whose values increase".
Please help to fix this

Md. Rahman

Served my purpose perfectly. Easy to use and well documented.

Leon

Exactly what I need! Many thanks for sharing this program.

If your goal is to save a figure and reload it later, here it is how to do it:
% Save it:
newFig = figure();
figHandle = copyUIAxes(app.UIAxes, newFig)
savefig(figHandle, 'AA.fig');

% Reload it:
figHandle = openfig('AA.fig');
axHandle = gca(figHandle);
copyobj(axHandle.Children, app.UIAxes)

It would be even better if the figure handle can be saved and reloaded without the creation of an independent figure.

Hope Q

Helpful workaround for copying uiaxes to axes object for the purpose of printing. I appreciate the options for legend and colorbar.

Hope Q

Updates

3.0.0

copyPosition input added to copy the axes position within the figure. Other minor changes.

2.0.0.1

Updated release compatibility fields; no changes to code.

2.0.0

Updated for changes made to Matlab r2020a

1.2.0.1

Announcement: Update for r2020a coming soon.

1.2.0

Now works with categoricalHistograms; new warning message appears if there's a problem copying a property; drawnow() added prior to copying; axis visibility is set at the end only when the UIAxis visibility is 'on'.

1.1.1

Changed thumbnail image only.

1.1.0

Destination figure handle returned in output structure; improved error handling; improved listIgnoredProps text.

1.0.1

Edited description; no change to code.

MATLAB Release Compatibility
Created with R2019b
Compatible with R2016a to R2020a
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: export_fig