Plot last point of column

3 views (last 30 days)
Ruben Garcia Paba
Ruben Garcia Paba on 21 Jun 2022
Commented: William Rose on 23 Jun 2022
Hello,
I have an output file from fortran model. It have 30 time-steps with 201 depht nodes.
I want to plot the last node of every time-step in funcion of time.
I do know how to take the last value only.
Thanks!
load 'zch4.dat'
tl=201; % Number of depth levels, should be 111. Otherwise check in data
[m,n]=size(zch4); % derermining the size of the data m=concentration, n=depht
% Number of profiles to be plotted
mm=m/tl; % total number of profiles
sm=mm; % tottal profiles
maxDepth = -300;
Time= 0:10:300 %300 years every 10 years
nexttile %CH4 diss
for i=1:30
d=maxDepth ;
hold on
x= 1e6*zch4; % <-- here variable concentration microMole => 1e9 miliMol=>1e6
plot(Time, ( x ((i-1)*tl+1:(i-1)*tl+tl,1)), '', 'LineWidth',1.0) %x=time, Y= i want last value of the every step.
hold off
xlabel ({'Time (years)'}) %label for variable and units
ylabel ({'CH_4 (aq) (mM)'})
% set(gca,'XTick', 0:max(x)/2:max(x) ) %choose the x axis ticks label
set(gca,'XAxisLocation','top')
box on
end

Answers (3)

KSSV
KSSV on 22 Jun 2022
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1040940/zch4.txt') ;
A = table2array(T) ;
[r,c] = size(A);
nlay = r/201;
B = permute(reshape(A',[c,r/nlay,nlay]),[2,1,3]);
iwant = squeeze(B(end,:,:))' ;

William Rose
William Rose on 22 Jun 2022
Your script has an error at line
plot(Time, ( x ((i-1)*tl+1:(i-1)*tl+tl,1)), '', 'LineWidth',1.0)
because Time is 1 by 31 and x((i-1)*tl+1:(i-1)*tl+tl,1) is 201 by 1. They must be the same size.
I am not sure exactly what you are trying to plot, and I am not sure how many plots you are trying to create. Your data file has 2 columns and 6030 rows. It appears there are 30 sets of 201 rows. You say that you have 31 time values, but there are only 30 sets of 201 values. You have a for loop that loops 30 times, and generates a new plot every time. Is it your intent to generate a single figure with 30 plots? Or do you want 1 plot with 30 points?
You may find it useful to copy column 1 to an array, and reshape it to have dimensions into a 201 x 30, as shown below:
load('zch4.txt');
tl=201; % Number of depth levels, should be 111. Otherwise check in data
[m,n]=size(zch4);
mm=m/tl;
a=reshape(zch4(:,1),tl,mm); %copy column 1 data into array a, and reshape it.
size(a)
ans = 1×2
201 30
This creates array a, with the data from column 1. The array is 201 rows by 30 columns. Now you can plot the 30 points from the first second , third, and final rows. Is this what you wanted to do?
Time=10:10:300;
plot(Time,a(1,:),'-r.');
hold on;
plot(Time,a(2,:),'-gx');
plot(Time,a(3,:),'-b+');
plot(Time,a(70,:),'-co');
plot(Time,a(140,:),'-m^');
plot(Time,a(end-1,:),'-yv');
plot(Time,a(end,:),'-kd');
legend('Level 1','Level 2','Level 3','Level 70','Level 140','Level 200','Level 201')
xlabel('Time (years)'); ylabel('CH_4 Concentration');
grid on;
Try it. Good luck.
  1 Comment
William Rose
William Rose on 22 Jun 2022
You can also plot the data at all 201 levels, at different times:
load('zch4.txt');
tl=201;
Level=1:tl;
Time=10:10:300;
[m,n]=size(zch4);
mm=m/tl;
a=reshape(zch4(:,1),tl,mm);
plot(Level,a(:,1),'-r.');
hold on;
plot(Level,a(:,2),'-gx');
plot(Level,a(:,3),'-b+');
plot(Level,a(:,10,:),'-co');
plot(Level,a(:,20,:),'-m^');
plot(Level,a(:,end-1),'-yv');
plot(Level,a(:,end),'-kd');
legend('T=10','T=20','T=30','T=100','T=200','T=290','T=300')
xlabel('Level'); ylabel('CH_4 Concentration');
grid on;
Or you could make a surface plot:
figure;
surf(Time,Level,a,'EdgeColor','none');
xlabel('Time (years)'); ylabel('Level'); zlabel('[CH_4]');
Try it.

Sign in to comment.


Ruben Garcia Paba
Ruben Garcia Paba on 22 Jun 2022
Edited: Ruben Garcia Paba on 22 Jun 2022
Thanks for the responce.
What I want to plot is the last step only. Every array represents te inpunt of methane on my system at time 0,1,2,3... in depht.
I want to plot is how the concentration of methane on the last point of the column vary with tyme.
I need a linear function, Concentration vs time. In depth 300.
  3 Comments
Ruben Garcia Paba
Ruben Garcia Paba on 22 Jun 2022
Yes, this is exactly what I need! I really apreciate youre help!!
Thank you very much!
William Rose
William Rose on 23 Jun 2022
@Ruben Garcia Paba, you're welcome. If this answers your question, please accept y answer. Thank you, and good luck with your work.

Sign in to comment.

Categories

Find more on Polar Plots in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!