Plot max, min, mean of a value in a matrix

I have a .txt file with all the temperature of a city in different years. I need to plot maximum, minimum and medium of one month from input, per each year. Example:
input month = 8
month 8 year 1996 --> max, min, mean
month 8 year 1997 --> max, min, mean
month 8 year 1998 --> max, min, mean
(all in one graph)
I've tried this:
clear all
%load file in a matrix
mt=load('LONDON.txt');
%I save only positive value because sometimes the file doesn't have value in a row
pos = mt(mt(:,4) > 0,:);
%input from keyboard of the month that I want to plot
month= input('\n enter a month\n');
%new matrix with all the positive value of the chosen month
fin = pos(pos(:,1) == month,:);
%matrix value in a table
T = array2table(fin, 'v',{'month','day','year','temperature'});
%value of each year
res95 = T(T.month==1995,:);
res96 = T(T.month==1996,:);
res97 = T(T.month==1997,:);
res98 = T(T.month==1998,:);
res99 = T(T.month==1999,:);
res00 = T(T.month==2000,:);
res01 = T(T.month==2001,:);
res02 = T(T.month==2002,:);
res03 = T(T.month==2003,:);
res04 = T(T.month==2004,:);
res05 = T(T.month==2005,:);
res06 = T(T.month==2006,:);
res07 = T(T.month==2007,:);
res08 = T(T.month==2008,:);
res09 = T(T.month==2009,:);
res10 = T(T.month==2010,:);
res11 = T(T.month==2011,:);
res12 = T(T.month==2012,:);
res13 = T(T.month==2013,:);
res14 = T(T.month==2014,:);
now I have different tables only with the month chosen. How can I plot theme all in one graph ? Thank you.

3 commentaires

Birdman
Birdman le 24 Oct 2017
Can you share the txt file?
giasco
giasco le 24 Oct 2017
this is the file
res95 = T(T.month==1995,:);
res96 = T(T.month==1996,:);
res97 = T(T.month==1997,:);
res98 = T(T.month==1998,:);
...
splitting data up into numbered variables is the start of writing complex, slow, buggy code. Data should be kept together as much as possible. In this case, calculating those values directly from the original array will be much simpler, e.g. using accumarray. A table could also be useful, but in the code shown serves no real purpose.
Read this to know why avoiding numbered variables is a good idea:

Connectez-vous pour commenter.

 Réponse acceptée

KL
KL le 24 Oct 2017
Modifié(e) : KL le 24 Oct 2017
Using a timetable makes this a lot easier
filename = 'LONDON';
data = readtable([filename,'.txt']);
data = data(data{:,4}>=0,:); %remove negative
choice_year = 1995;
data.dt = datetime(data{:,[3 1 2]});
data_TT = timetable(data.dt,data{:,4});
data_monthly_mean= retime(data_TT(data_TT.Time.Year==choice_year,:),'monthly','mean');
data_monthly_max = retime(data_TT(data_TT.Time.Year==choice_year,:),'monthly','max');
data_monthly_min = retime(data_TT(data_TT.Time.Year==choice_year,:),'monthly','min');
figure(1)
plot(data_monthly_mean.Var1)
hold on
plot(data_monthly_max.Var1)
plot(data_monthly_min.Var1)
hold off
legend({'mean','max','min'})
title([filename ' monthly analysis for ' num2str(choice_year)])

3 commentaires

giasco
giasco le 24 Oct 2017
If I want extract only a month per year ?
KL
KL le 24 Oct 2017
check the edited answer:)
giasco
giasco le 24 Oct 2017
thank you !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by