How to fix this code trying to create 3 seperate plots from a 3D plots exported to png.

4 views (last 30 days)
This is code to take all fig files from my map and to convert the to a png, with exception of a 3DPlot, which I want to export as 3 different figures for all axis combinations : X-Y X-Z and Y-Z
the Z axis is not found though.
I made most of this code with AI and am a novice when it comes to coding,
As far as I understand its not finding 3 axis, returning i = 1
returning the following error:
Index exceeds the number of array elements (1).
Error in Fig_to_png_Script (line 27)
copyobj(get(axesHandles(2), 'Children'), ax2);
I added Numel exceptions in order to skip the error in this version so the code runs and creates only the X-Y plot
% Get all .fig files in the current directory
figFiles = dir('*.fig');
% Loop through each .fig file
for i = 1:length(figFiles)
figName = figFiles(i).name;
% Check if it's a 3D plot
if contains(figName, '3DPlot.fig', 'IgnoreCase', true)
% Load the figure
fig = openfig(figName, 'invisible');
% Get handle to the axes
axesHandles = findobj(fig, 'type', 'axes');
% Handle the X-Y plane (axis 1)
if numel(axesHandles) >= 1
newFig = figure;
ax1 = axes('Parent', newFig);
copyobj(get(axesHandles(1), 'Children'), ax1);
xlabel(ax1, get(get(axesHandles(1), 'XLabel'), 'String'));
ylabel(ax1, get(get(axesHandles(1), 'YLabel'), 'String'));
[~, figBaseName, ~] = fileparts(figName);
saveas(newFig, [figBaseName '_axis_1.png']);
close(newFig);
end
% Handle the X-Z plane (axis 2)
if numel(axesHandles) >= 2
newFig = figure;
ax2 = axes('Parent', newFig);
copyobj(get(axesHandles(2), 'Children'), ax2);
xlabel(ax2, get(get(axesHandles(2), 'XLabel'), 'String'));
zlabel(ax2, get(get(axesHandles(2), 'ZLabel'), 'String'));
[~, figBaseName, ~] = fileparts(figName);
saveas(newFig, [figBaseName '_axis_2.png']);
close(newFig);
end
% Handle the Y-Z plane (axis 3)
if numel(axesHandles) >= 3
newFig = figure;
ax3 = axes('Parent', newFig);
copyobj(get(axesHandles(3), 'Children'), ax3);
ylabel(ax3, get(get(axesHandles(3), 'YLabel'), 'String'));
zlabel(ax3, get(get(axesHandles(3), 'ZLabel'), 'String'));
[~, figBaseName, ~] = fileparts(figName);
saveas(newFig, [figBaseName '_axis_3.png']);
close(newFig);
end
else
% Load the figure
fig = openfig(figName);
% Save as PNG
saveas(fig, [figName '.png']);
% Close the figure
close(fig);
end
end

Accepted Answer

Voss
Voss on 19 Apr 2024
Edited: Voss on 19 Apr 2024
The error message
Index exceeds the number of array elements (1).
on the line
copyobj(get(axesHandles(2), 'Children'), ax2);
indicates that axesHandles only has one element. That is, even your 3DPlot figure only has one axes object.
(Confusion may arise from the fact that in MATLAB an axes object is a graphics object for plotting which may be viewed in 2d or 3d. So it's one "axes"; it contains an x-axis, y-axis, and a z-axis, but these are not objects of type "axes" but rather "NumericRuler".)
To save the X-Y, X-Z, and Y-Z views of the axes into three separate files, you can do as follows:
% Get all .fig files in the current directory
figFiles = dir('*.fig');
% Loop through each .fig file
for i = 1:length(figFiles)
figName = figFiles(i).name;
% Check if it's a 3D plot
if contains(figName, '3DPlot.fig', 'IgnoreCase', true)
% Load the figure
fig = openfig(figName, 'invisible');
% Get handle to the axes
ax = findobj(fig, 'type', 'axes');
% Create new figure and axes
newFig = figure;
ax1 = axes('Parent', newFig);
% Copy stuff from old axes to new axes
copyobj(ax.Children, ax1);
% set the new axes labels
ax1.XLabel.String = ax.XLabel.String;
ax1.YLabel.String = ax.YLabel.String;
ax1.ZLabel.String = ax.ZLabel.String;
% save the 3 views to files
[~, figBaseName, ~] = fileparts(figName);
view(ax1,[0 90]) % xy view
saveas(newFig, [figBaseName '_axis_1.png']);
view(ax1,[0 0]) % xz view
saveas(newFig, [figBaseName '_axis_2.png']);
view(ax1,[90 0]) % yz view
saveas(newFig, [figBaseName '_axis_3.png']);
% Close the figure
close(newFig);
else
% Load the figure
fig = openfig(figName);
% Save as PNG
saveas(fig, [figName '.png']);
% Close the figure
close(fig);
end
end

More Answers (0)

Categories

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

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!