Exporting Graphs from Matlab App in PNG or JPG
13 views (last 30 days)
Show older comments
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
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')
Answers (3)
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')
0 Comments
Image Analyst
on 10 Sep 2024
Assuming it's the current/active figure, try
exportgraphics(gcf, 'ECRTools.png');
in the command window.
help exportgraphics
0 Comments
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.
0 Comments
See Also
Categories
Find more on Printing and Saving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!