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

6 Ansichten (letzte 30 Tage)
Adam Rajcan
Adam Rajcan am 25 Feb. 2021
Bearbeitet: Veronica Taurino am 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 Kommentare
Adam Rajcan
Adam Rajcan am 25 Feb. 2021
Edit: It actually works for the first plot, but not for the later 3, which are shown. For these it saves only the first frame, but instead of i (which is the for cycle count) it saves under number 56 and then it does not do anything.
Here is the code for the first one that actually works.
figure('units','normalized','outerposition',[0 0 1 1]);
surfPlot = surf(X_grid,Y_grid,Z_grid,'FaceAlpha',0.9);
colorbar
title('Ochýlka rozložená na plochu obrazu')
xlabel('Šírka snímky')
ylabel('Výška snímky')
zlabel('Projekčná odchýlka')
surfPlot.EdgeColor = 'none';
outputBaseFileName = sprintf('Frame %d.jpg',i);
outputSaveFile_Error = 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\error',outputBaseFileName);
saveas(gcf,outputSaveFile_Error)
close all
Adam Rajcan
Adam Rajcan am 25 Feb. 2021
Bearbeitet: Adam Rajcan am 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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Veronica Taurino
Veronica Taurino am 25 Feb. 2021
Bearbeitet: Veronica Taurino am 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

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by