How to fix this code trying to create 3 seperate plots from a 3D plots exported to png.
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
endThis 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 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 plot, getobj, 3dplot, error MATLAB Answers — New Questions