Plot last point of column
Show older comments
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
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,:,:))' ;
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)
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
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.
Ruben Garcia Paba
on 22 Jun 2022
Edited: Ruben Garcia Paba
on 22 Jun 2022
0 votes
3 Comments
The first plot I provided in my initial answer shows CH4 versus time at level 201. Here is code that plots CH4 concentration versus time, for level 201 only, without any other levels.
load('zch4.txt');
tl=201; % Number of depth levels
[m,n]=size(zch4);
mm=m/tl;
a=reshape(zch4(:,1),tl,mm);
Time=10:10:300;
plot(Time,a(end,:),'-kd');
legend('Level 201')
xlabel('Time (years)'); ylabel('CH_4 Concentration');
grid on;
You wrote "Every array represents te inpunt of methane on my system at time 0,1,2,3..." In your code and comments, you said the times are 0, 10, 20, ..., 300, which makes 31 times. However, your data has 30 groups, not 31. Therefore I assume Time=10:10:300. Do you want Time=0:29? Or Time=1:30?
You requested concentration versus time at depth 300. I assume that depth 300 corresponds to level 201. I also assume that level 201 data is on rows 201, 402, 603, ..., 6030 in file zch4.txt. I also assume that rows 1-201 of file zch4.txt are time=10, rows 202-402 are time=20, ..., rows 5830-6030 are time=300. If that is incorrect, then please specify which rows in file zch4.txt correspond to depth 300. If that is incorrect, then please specify exactly which rows of the original file correspond which which times and levels.
This whole process would be easier if you added a column for time and a column for depth to your original text file. Then it would be clear which samples correspond to which times and depths.
Ruben Garcia Paba
on 22 Jun 2022
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.
Categories
Find more on Surface and Mesh Plots 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!


