saveas won't save my figure when in a for cycle

1 view (last 30 days)
Adam Rajcan
Adam Rajcan on 25 Feb 2021
Edited: Veronica Taurino on 25 Feb 2021
Dear Matlab comunity,
I run into a problem with a command saveas. I have a for loop in which I create a set of surf plots, which I would like to save independently and for each cycle. When I run just the part with the surf plots, it saves just as intended. But when I run the whole for loop, it does not. Any suggestions?
The surf plot is a part of a one big for loop, so I post just the code that applies to surf plots. In this part 3 different surf plots are ought to be plotted and there is one more just like these 3 couple dozens lines earlier.
%% Plotting error surface plot
% In this section the distance between the points is plotted
figure('units','normalized','outerposition',[0 0 1 1]);
surfPlot = surf(X_grid,Y_grid,delta_x,'FaceAlpha',0.9);
colorbar
zlim([4.5 5.5])
title('Vzdialenosti medzi bodmi v x smere')
xlabel('Šírka snímky')
ylabel('Výška snímky')
zlabel('\Delta x medzi bodmi')
surfPlot.EdgeColor = 'none';
outputBaseFileName = sprintf('Frame %d.jpg',i);
outputSaveFile_sumX = fullfile('C:\Users\Adam\Documents\Škola\Uniza\Diplomová práca\8. Finálne meranie auta\Zabery\calibration images 50mm\Kalibracia 50mm side to side\Results\delta_x',outputBaseFileName);
saveas(gcf,outputSaveFile_sumX)
close all
figure('units','normalized','outerposition',[0 0 1 1]);
surfPlot = surf(X_grid,Y_grid,delta_y,'FaceAlpha',0.9);
colorbar
zlim([4.5 5.5])
title('Vzdialenosti medzi bodmi v y smere')
xlabel('Šírka snímky')
ylabel('Výška snímky')
zlabel('\Delta y medzi bodmi')
surfPlot.EdgeColor = 'none';
outputBaseFileName = sprintf('Frame %d.jpg',i);
outputSaveFile_sumY = fullfile('C:\Users\Adam\Documents\Škola\Uniza\Diplomová práca\8. Finálne meranie auta\Zabery\calibration images 50mm\Kalibracia 50mm side to side\Results\delta_y',outputBaseFileName);
saveas(gcf,outputSaveFile_sumY)
close all
figure('units','normalized','outerposition',[0 0 1 1]);
surfPlot = surf(X_grid,Y_grid,delta_sum,'FaceAlpha',0.9);
colorbar
zlim([6.5 7.5])
title('Absolutne vzdialenosti medzi bodmi')
xlabel('Šírka snímky')
ylabel('Výška snímky')
zlabel('\Delta sum medzi bodmi')
surfPlot.EdgeColor = 'none';
outputBaseFileName = sprintf('Frame %d.jpg',i);
outputSaveFile_sum = fullfile('C:\Users\Adam\Documents\Škola\Uniza\Diplomová práca\8. Finálne meranie auta\Zabery\calibration images 50mm\Kalibracia 50mm side to side\Results\delta_sum',outputBaseFileName);
saveas(gcf,outputSaveFile_sum)
close all
  5 Comments
Adam Rajcan
Adam Rajcan on 25 Feb 2021
Just as I was about to post the whole for loop I spotted that I have another for loop in the middle of it using the same iteration counter i=1:56, which resulted into saving all the later plots under frame number 56 instead of using the correct iteration count. That was the reason.
Sorry for posting such a mistaken script, but thank you for your help. Now it works fine.
for i=1:boardSize(1,2)-1-StepSize % Coloumns
for k=1:boardSize(1,1)-1-StepSize % Rows
delta_x(k,i) = X_me(k,i+StepSize) - X_me(k,i); % Distance between the points along the x axis
delta_y(k,i) = Y_me(k+StepSize,i) - Y_me(k,i); % Distance between the points along the x axis
delta_sum(k,i) = sqrt(delta_x(k,i).^2 + delta_y(k,i).^2); % Absolute distance between the points
end
end

Sign in to comment.

Answers (1)

Veronica Taurino
Veronica Taurino on 25 Feb 2021
Edited: Veronica Taurino on 25 Feb 2021
Something like this within each loop?
FolderName='path to save your figures here'
FigList = findobj('Type', 'figure');
for iFig = 1:length(FigList)
FigHandle = FigList(iFig);
FigName = get(FigHandle, 'Name');
saveas(FigHandle, fullfile(FolderName, [FigName '.jpg']));
end

Tags

Community Treasure Hunt

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

Start Hunting!