save figure as filename that I already have

1 view (last 30 days)
I made a plot with 3 y axis
ngll = 5;
DATA_DIR = './Snap';
db_dir = 'fault';
fault = 1;
precision = 'double';
isnap=20000;
YLIM = [-50 50]; % range along-strike
XLIM = [0 50]; % range along-depth
d = FSEM3D_snapshot(isnap,DATA_DIR,fault,precision);
%this file contains the initial parameter. It must be the same
FaultinitparaF1 = import_Fault_init_para_F1('Fault_init_para_F1.txt', 1, 89704);
X=(FaultinitparaF1(:,1));
Y=(FaultinitparaF1(:,2));
Z=(FaultinitparaF1(:,3));
S1=(FaultinitparaF1(:,4)).*10^-6;
S2=(FaultinitparaF1(:,5)).*10^-6;
S3=(FaultinitparaF1(:,6)).*10^-6;
mus=(FaultinitparaF1(:,7));
mud=(FaultinitparaF1(:,8));
Dc=(FaultinitparaF1(:,9));
middepth=Z(Y==0);
[middepth_sorted,middepth_order]=sort(middepth);
s1=S1(Y==0);
s2=S2(Y==0);
s3=S3(Y==0);
Tx=(d.Tx).*10^-6; %strikeslip
Tz=(d.Tz).*10^-6;
Ty=(d.Ty).*10^-6; %dipslip
TX=Tx(Y==0);
TZ=Tz(Y==0);
TY=Ty(Y==0);
shearstressChange=s1-TX;
sstressChange=shearstressChange(middepth_order,:);
normalstressChange=s3-TZ;
nstressChange=normalstressChange(middepth_order,:);
alongdipstressChange=s2-TY;
dipstressChange=alongdipstressChange(middepth_order,:);
x=sort((d.Z(d.Y==0))/sind(60),'descend'); %distance along dip
yl1 = dipstressChange;
yl2 = nstressChange;
yyaxis left
Line1=plot(x,yl1,'LineWidth',3);
xlabel('Distance Downdip [km]')
ylabel('Stress Change [MPa]')
title('0.5s')
grid on
ylim([-10 10])
hold on
Line2=plot(x,yl2,'-g','LineWidth',1);
Vz = ((d.Vz(d.Y==0)));
yr1 = Vz(middepth_order,:);
yyaxis right
Line3=plot(x,yr1,'r');
ylim([-3 3])
ylabel('Slip Velocity [m/s]')
% plot(x,yr2)
hold off
xlim([-50 0])
asptop = -9*[1;1];
aspdown = -15*[1;1];
hold on
plot([asptop aspdown],ylim,'k','LineWidth',2)
hold off
saveas(gcf,'20s.png')
saveas(h,[pwd '\' Savefilename Savefileextension],'jpg');
hL = legend([line,line,line],{'alongdip\_stresschange','normal\_stresschange','Slip Velocity'});
**** now I have a few question and would be very thankful if you help me with your tips
please look at the attched script
1. I want to have the legend, but I wont get the line colors that I specified in my plot color
2. In the begining of the script I have
isnap=20000;
around the middle
title('20s')
and at the end of the file I have
saveas(gcf,'20s.png')
20 is my isnap*0.001 and add an 's'.
I have to run this script more than 10 times,how can I extract filename automatically
  2 Comments
Geoff Hayes
Geoff Hayes on 24 Jun 2019
Samaneh - for your first question, I want to have the legend, but I wont get the line colors that I specified in my plot color, it could be that you are plotting more objects than you think and this could be due to the dimensions of the data that you are passing into plot. For each call to plot check the following:
  • what are the dimensions for the two input parameters? Are the input parameters arrays or matrices?
  • what are the dimensions for the output objects? Is it a scalar or an array?
For example,
Line1=plot(x,yl1,'LineWidth',3);
Line1 will have the handles to all graphics objects created when calling plot. Is Line1 a scalar (single handle to a graphics object) or is it an array (of handles to multiple graphics objects). If the latter, then this could explain why your legend is not showing the correct colours: you are plotting more graphics obejcts than your legend is expecting and so you will need to verify that your inputs (to plot) are what you expect (use the MATLAB debugger to validate all of this).
For your second question, I have to run this script more than 10 times,how can I extract filename automatically the code you could use to create a file name might be
isnap=20000;
% your code
filename = sprintf('%ds.png',isnap/1000);
I'm assuming that isnap is always an integer.
Samaneh Arzpeima
Samaneh Arzpeima on 24 Jun 2019
Thank you
yl1,yl2,yr1,x are all 220*1 vectors
inap is 0:500:30000

Sign in to comment.

Accepted Answer

per isakson
per isakson on 24 Jun 2019
Edited: per isakson on 24 Jun 2019
Q1: I've learned the hard way not to mess with legend(). Thus, I pass this one.
Q2: Here is a hint. Run this script one %%-section at a time
%%
isnap = 20000;
snap_name = sprintf( sprintf( '%ds', round(isnap/1000) ) );
%%
title( snap_name );
saveas( gcf, [snap_name,'.png'])
However, this doesn't work for title('0.5s'). Before trying cover all cases a better specification is needed.

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!