How to save multiple figures into a folder on desktop from a for loop?

6 views (last 30 days)
Hello,
I am trying to save all my figures, but don't want to run my code and then save each individual figure, can you help me speed up my process, thanks. Below is a copy of my code, I care about the for loop where z_plot is; that is outputing my figures. Also, I have written a lot of functions so if you try copying it and running it, it might give you issues.
-Robert
Safety_Factor = 2.5;
E1 = 190*10^9;
E2 = 9.9*10^9;
v12 = 0.35;
v21 = E2*(v12/E1);
G12 = 7.8*10^9;
alpha1 = -0.9*10^-6;
alpha2 = 22*10^-6;
F_1t = 3250*10^6; % Longitudinal Tensile Strength, measured in MPa
F_1c = -1590*10^6; % Longitudinal Compression Strength
F_2t = 62*10^6; % Transverse Tensile Strength
F_2c = -200*10^6; % Transverse Compression Strength
F_6 = 75*10^6; % In-Plane Shear Strength
Ultimate_ep_1t = 0.016;
Ultimate_ep_1c = abs(F_1c/E1);
Ultimate_ep_2t = .006;
Ultimate_ep_2c = abs(F_2c/E2);
Ultimate_ep_shear = abs(F_2t/G12);
Q = Q_Matrix(E1,E2,v12,v21,G12);
Angles = input('Insert Angles in Matrix Form ')
Thickness = input('Insert Thickness of Ply ')
Number_of_Plys = length(Angles);
Failed = zeros(Number_of_Plys,1);
Delta_Temp = input('Insert Temperature Difference ')
Thermal_Strain = Delta_Temp*[alpha1; alpha2; 0];
first = 0;
rerun = 1;
while rerun == 1
rerun = 0;
[ABD, Thermal_Load] = ABD_Matrix_and_Thermal_Loads(Angles,Thickness,Failed,E1,E2,v12,v21,G12,Thermal_Strain);
%Force_Load = [0.6*10^6;0.3*10^6;0;0.3;0;0]*Safety_Factor;
%Force_Load = [0;0;0.1;0;0.3;0]*Safety_Factor;
%Force_Load = [0;0;0;0;0;0.3]; % For this one multiply just the total load by the safety factor
%Force_Load = [0;0;0;10;0;0];
Total_Load = Force_Load + Thermal_Load
[Stress_XY, Strain_XY, Stress_12, Strain_12] = All_Stresses_and_Strains(ABD,Thickness,Angles,Total_Load,E1,E2,v12,v21,G12,Failed);
Stress_XY;
Stress_12;
Strain_XY;
Strain_12;
% Max Stress Failure Criteria
Fail_Sum_Old = sum(Failed);
[Failed_out] = Max_Stress_Failure_Criteria(Stress_12,Failed,F_1t,F_1c,F_2t,F_2c,F_6);
Failed_out
Fail_Sum_New = sum(Failed_out);
% if Fail_Sum_New > Fail_Sum_Old && Fail_Sum_New ~= 3*length(Failed)
% rerun = 1;
% Failed = Failed_out;
% end
% Hashim-Rotem
Failed;
Fail_Old_Sum = sum(Failed);
[Failed_Out_HR] = Hashin_Rotem_Failure_Criteria(Stress_12,F_6,F_2t,F_2c,F_1t,F_1c,Failed,Ultimate_ep_shear,Ultimate_ep_1t,Ultimate_ep_1c,Ultimate_ep_2t,Ultimate_ep_2c);
Failed_Out_HR
Fail_New_Sum = sum(Failed_Out_HR);
if Fail_New_Sum > Fail_Old_Sum && Fail_New_Sum ~= 3*length(Failed)
rerun = 1;
Failed = Failed_Out_HR;
end
% if first == 0
for i=1:3
z_plot(Strain_12(i,:),Strain_XY(i,:),Stress_XY(i,:),Stress_12(i,:),Thickness,(i-1))
end
% first = 1;
% z_plot
% Failed_out
% ABD
% end
end

Accepted Answer

Adam Danz
Adam Danz on 11 Dec 2018
Edited: Adam Danz on 11 Dec 2018
Here's a function that saves all available figures to a specified destination and you can also specify the file extension (fig, jpg, etc.). The file names for each figure will optionally have a datetime stamp to prevent overwriting previous figures. If you want to overwrite previous figures you can turn off the datetime stamp.
To use this, make sure all previous figures are cleared before running your code because this will save all available figures. Then run this after the z_plot loop.
Here's an example:
destination = 'C:\Users\name\Documents\MATLAB';
ext = {'fig', 'jpg'};
saveallfigs(destination, ext);
Alternatively, if z_plot returns a handle to your plot, you could just save each plot within the i-loop but make sure each file has its own filename to prevent overwriting.
  5 Comments
Robert  Flores
Robert Flores on 12 Dec 2018
This is my first time making one of these "saving loops", so I need a little bit more help. For the path I think it would just be 'C:\Desktop\Composites_Project\Test', but I don't think this is right. I have a windows computer, so will I have the '\Users\name\...'. Thanks for all your help so far!
Adam Danz
Adam Danz on 12 Dec 2018
The easiest way to define the path is to open the folder where the figures should be saved, copy the path, and then paste it directly into matlab.
181212 090200-MATLAB.jpg
destination = 'C:\Users\adanz\Documents\MATLAB';

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!