Exporting Graphs from Matlab App in PNG or JPG

8 views (last 30 days)
Dario
Dario on 10 Sep 2024
Commented: Dario on 14 Sep 2024
I'm using an app to fit some experimental data to model. The app wasn't written by myself. After fitting, few different graphs are shown with the results. I don't see the option to export these graphs in PNG or JPG or any other format for that matter inside the app window. Do you know how can one export graphs from this Matlab app.
Here is the app window shown. Usually, with graphs, there is an export button on the axes toolbar. However, none exist here.
  6 Comments
DGM
DGM on 11 Sep 2024
Edited: DGM on 11 Sep 2024
The arguments passed to save() should be variable names, but boundary_cov(2,:) isn't a variable itself. This limitation makes more sense when you expect the output of save() to be a .mat file, wherein the specified variables are stored with their original names.
You could create new variable names, but since you're using save() with ascii mode output where the names are just discarded, these two row vectors are written on sequential lines in the output file exactly as if they were a 2D array. Without names, they're just two adjacent row vectors in the same order as they were originally.
boundary_cov_x = boundary_cov(1,:);
boundary_cov_y = boundary_cov(2,:);
save('conf.txt','boundary_cov_x','boundary_cov_y','-ascii')
Instead, you can just save boundary_cov as a whole 2D array. This produces the same result without the extra steps.
save('conf.txt','boundary_cov','-ascii')
Dario
Dario on 14 Sep 2024
Oh, okay. Basically, the whole variable can only be stored and not only part of it. Thank you for the help as it worked for me.

Sign in to comment.

Answers (3)

ScottB
ScottB on 10 Sep 2024
One way to export to png is with the print command:
% Export to graphics file
savename = 'MyFile';
print(gcf,savename,'-dpng')

Image Analyst
Image Analyst on 10 Sep 2024
Assuming it's the current/active figure, try
exportgraphics(gcf, 'ECRTools.png');
in the command window.
help exportgraphics
EXPORTGRAPHICS Save plot or graphics content to file EXPORTGRAPHICS(OBJ, FILESPEC) saves the specified graphics to a file. OBJ is the handle of any type of an axes, a figure, a chart that can be a child of the figure, a tiled chart layout, or a container within the figure. FILESPEC is a character vector or string scalar specifying the name of a file to create. It must include the extension, and can optionally include a full or relative path. EXPORTGRAPHICS supports creating JPEG, PNG, and TIFF images, and EPS, PDF, and EMF vector formats (EMF is only supported on Windows). EXPORTGRAPHICS(___, OPTIONS) saves the graphics to the file, applying the specified OPTIONS. OPTIONS is one or more of the following name-value pairs. 'ContentType', CONTENTTYPE how to generate content in vector format files CONTENTTYPE can be one of the following values - 'vector' to generate vectorized (scalable) output - 'image' to generate rasterized output (embed image) - 'auto' allow MATLAB heuristic to decide The default value is 'auto' 'Resolution', DPI specifies output resolution for images. DPI must be a positive whole number. The default value is 150 'BackgroundColor', COLORSPEC specifies the color to use as the background COLORSPEC can be one of the following values - a color name, such as 'red', or 'w' - an RGB triplet, such as [1 0 0] or [1 1 1] - a hexadecimal color code, such as '#FFF' or '#FFFFFF') - 'none' to use either a white or transparent background, depending on what the output format supports - 'current' to use the graphic's current background color The default value is 'white' 'Colorspace', COLORSPACE the color space to use when generating output COLORSPACE can be one of the following values - 'rgb' generates RGB true color output - 'gray' generates grayscale output - 'cmyk' generates cyan, magenta, yellow, black output The default value is 'rgb' 'Append', APPEND specifies whether to append the output to an existing file or replace the content. APPEND can have a value of true or false. - true appends the output to an existing file - false replaces the content in an existing file The default value is false Example: Export a plot to a pdf file plot(rand(3)); exportgraphics(gca, 'results.pdf'); Example: Export a plot to a pdf file and ensure output is vectorized/scalable plot(rand(3)); exportgraphics(gca, 'results.pdf', 'ContentType', 'vector'); Example: Export a plot as an image to a pdf file plot(rand(3)); exportgraphics(gca, 'results.pdf', 'ContentType', 'image'); Example: Export a plot and append to an existing pdf file plot(rand(3)); exportgraphics(gca, 'results.pdf', 'Append', true); Example: Export a specific plot from the figure fig = figure; ax1 = subplot(2,1,1); bar(magic(4)); ax2 = subplot(2,1,2); plot(rand(4)); exportgraphics(ax2, 'results.png'); Example: Export all plots from figure to an image file at 300DPI subplot(2,1,1); bar(magic(4)); subplot(2,1,2); plot(rand(4)); exportgraphics(gcf, 'results.png', 'Resolution', 300); Example: Export a plot to an image file with a red background plot(rand(3)); exportgraphics(gca, 'results.png', 'BackgroundColor', [1 0 0]); Example: Export a plot to a grayscale image file plot(rand(3)); exportgraphics(gca, 'results.png', 'Colorspace', 'gray'); See also COPYGRAPHICS, EXPORTAPP Documentation for exportgraphics doc exportgraphics

DGM
DGM on 10 Sep 2024
Edited: DGM on 10 Sep 2024
Using gcf() won't work. The GUI handles aren't immediately visible, so calling gcf will just open and capture an empty figure.
This will capture the entire figure:
% find all the axes objects in the GUI figure
hf = findobj(findall(0),'type','figure','name','ECRTOOLS_GUI')
% save a screenshot of the figure
thistime = datestr(now,'yyyy-mm-dd_hh:MM:ss');
thisname = sprintf('capturedgui_%s.png',thistime);
exportgraphics(hf,thisname)
This will capture the three axes as separate images instead:
% find all the axes objects in the GUI figure
hf = findobj(findall(0),'type','figure','name','ECRTOOLS_GUI')
hax = findobj(hf,'type','axes')
% save each axes as a separate image
for k = 1:numel(hax)
thistime = datestr(now,'yyyy-mm-dd_hh:MM:ss');
thisname = sprintf('capturedaxes_%s_%02d.png',thistime,k);
exportgraphics(hax(k),thisname)
end
Likewise, you could still fetch the plotted data externally. You just have to find it.
% find all axes objects in the GUI figure
hf = findobj(findall(0),'type','figure','name','ECRTOOLS_GUI');
hax = findobj(hf,'type','axes');
% sort through the axes to find the underlying data
% the main plot consists of three line objects in two overlaid axes
time = [];
sigma_n = [];
error_meas = [];
for k = 1:numel(hax)
% look for the axes corresponding to sigma_n
% this axes contains two line objects for sigma_n and sigma_n_meas
if strcmp(hax(k).YLabel.String,'$\sigma_n$')
hl = findobj(hax(k),'type','line');
time = hl(2).XData;
sigma_n = hl(2).YData; % hl(2) is sigma_n, not sigma_n_meas
% look for the axes for the error measurement
elseif strcmp(hax(k).YLabel.String,'$100\times(\sigma_n-\sigma_n^{\rm meas}~)$')
hl = findobj(hax(k),'type','line');
error_meas = hl.YData/100; % rescale back to unit-scale
end
end
% save the data
save('ECR.txt','time','sigma_n','error_meas','-ascii')
Again, all of these examples are being external to the GUI code.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!