copying displayed UIAxes to clipboard a/o create image file

13 visualizaciones (últimos 30 días)
Alexander Naundorf
Alexander Naundorf el 13 de Abr. de 2022
Editada: Hari el 11 de Oct. de 2023
Hi Guys and Girls,
atm im working on a GUI that displays a set of data in different UIAxes (as shown in picture below).
The six plots you can see in the picture are called app.UIAxes2_(2:7)
I want to add the functionality, that all of the UIAxes are saved at once either in clipboard or/and as an image file (I am aware that I could do that by Shft+Win+S, lets assume we dont have that option).
I experimented with tiledLayout (only 2 axes to simplify the problem)
sda = tiledlayout(2,1);
nexttile
app.UIAxes2_7
nexttile
app.UIAxes2_2
copygraphics(sda,'ContentType','image');
but I am having problems with 'forcing' in the UIAxes into the tiles. They simply do not display in the tiledlayout.
I also thought about creating a tiledImage. But it is really slow to copy every UIAxes to the clipboard and then adding it to the tiledImage (command 'copygraphics(app.UIAxes2_x,'ContentType','image');' takes ~ 5 secs for each UIAxes to copy.), so that isnt an option neither.
Do you guys have any idea on how to tackle this problem?
Greetings

Respuestas (1)

Hari
Hari el 11 de Oct. de 2023
Editada: Hari el 11 de Oct. de 2023
Hi Alexander,
I understand you are working on a GUI that displays data in different “UIAxes” and you want to add functionality to save all the “UIAxes” at once, either to the clipboard or as image files.
To achieve the desired behaviour, you can use the "copyobj" function, which copies the children of the "UIAxes" objects to the “tiledLayout”. Then, you can use the "saveas" function to save the "tiledLayout" as an image file. And to copy the images to the clipboard, you can use the "print" function with the '-clipboard' and '-dbitmap' options.
Here is a function to copy “UIAxes” objects as an image:
% Function to copy UIAxes objects as an image
function copyUIAxesAsImage(ax1, ax2)
% Create a tiledLayout
tiledLayout = tiledlayout(2, 1);
% Add UIAxes to the tiledLayout
nexttile;
copyobj(ax1.Children, gca);
nexttile;
copyobj(ax2.Children, gca);
% Save the tiledLayout as an image file
saveas(gcf, 'tiled_layout_image.png');
% Copy the tiledLayout to the clipboard
print(gcf, '-clipboard', '-dbitmap');
% Close the tiledLayout
delete(tiledLayout);
end
You can use this function to trigger the callback when the button is pressed on the GUI. Here is the sample code to call this function on ButtonPushed” event.
% Create a GUI figure
fig = uifigure('Name', 'UIAxes Copy Example', 'Position', [100 100 400 300]);
% Create UIAxes objects
ax1 = uiaxes(fig, 'Position', [50 150 300 100]);
ax2 = uiaxes(fig, 'Position', [50 50 300 100]);
% Generate some sample data
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
% Plot data on UIAxes objects
plot(ax1, x, y1);
title(ax1, 'UIAxes 1');
plot(ax2, x, y2);
title(ax2, 'UIAxes 2');
% Create a button to copy UIAxes as an image
copyButton = uibutton(fig, 'Position', [150 10 100 30], 'Text', 'Copy as Image', 'ButtonPushedFcn', @(btn,event) copyUIAxesAsImage(ax1, ax2));
When you run this code, you will see the GUI with the sine and cosine “UIAxes” with a "Copy as Image" button. When you click on the button “UIAxes” objects will be copied as images to the clipboard and you can find a “.png” file in your current directory containing all the images on the GUI.
Output observed:
Figure 1. GUI showing UIAxes with the button “Copy as Image” button.
Figure 2. Image saved in the current folder
Refer to the documentations of “titledLayout”, “copyObject”, and “saveas” functions for more information.
Hope this helps!

Categorías

Más información sobre Printing and Saving en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by