Plotting from an array at an interval

3 vues (au cours des 30 derniers jours)
Haukur Ægir Jóhannesson
Commenté : Star Strider le 1 Nov 2020
I am trying to plot two columns in an array. The x-axis represents months in the year and the y-axis is temperature values from each month. This data loops in my array in intervals of 12. I am trying to plot each year ( 12 months) as a seperate line on the plot. How can i do this?
My current code plots this as one line thorughout my plot.
for a = 1:length(skrar)
figure;
LH = load(skrar(a).name);
uni = unique(LH(:,2));
plot(LH(:,3),LH(:,4),'Color',rand(1,3));
title(LH(1,1) + " Median " + LH(1,2) + " til " + LH(end,2))
manudir = {'Jan';'Feb';'Mar';'Apr';'Maí';'Jún';'Júl';'Ágú';'Sep';'Okt';'Nóv';'Des'};
set(gca,'xtick',[1:12],'xticklabel',manudir)
ylabel('Hitastig[°C]')
end
An example of my data looks like this:
422 1882 1 -0.8 4
422 1882 2 -5.8 4
422 1882 3 -5.5 4
422 1882 4 -3.1 4
422 1882 5 2.0 4
422 1882 6 4.5 4
422 1882 7 6.1 4
422 1882 8 4.1 4
422 1882 9 5.3 4
422 1882 10 5.0 4
422 1882 11 -1.8 4
422 1882 12 -3.9 4
422 1883 1 -1.4 4
422 1883 2 -0.8 4
422 1883 3 -2.9 4
422 1883 4 3.0 4
422 1883 5 1.9 4
422 1883 6 8.9 4
422 1883 7 10.1 4
422 1883 8 7.7 4
422 1883 9 7.2 4
422 1883 10 2.9 4
422 1883 11 -0.2 4
422 1883 12 -1.0 4
Column 3 is the months and column 4 is the temperature
My current plot looks like this:

Réponse acceptée

Star Strider
Star Strider le 1 Nov 2020
There are likely any number of ways to do this.
One relatively straightforward way:
ExData = [422 1882 1 -0.8 4
422 1882 2 -5.8 4
422 1882 3 -5.5 4
422 1882 4 -3.1 4
422 1882 5 2.0 4
422 1882 6 4.5 4
422 1882 7 6.1 4
422 1882 8 4.1 4
422 1882 9 5.3 4
422 1882 10 5.0 4
422 1882 11 -1.8 4
422 1882 12 -3.9 4
422 1883 1 -1.4 4
422 1883 2 -0.8 4
422 1883 3 -2.9 4
422 1883 4 3.0 4
422 1883 5 1.9 4
422 1883 6 8.9 4
422 1883 7 10.1 4
422 1883 8 7.7 4
422 1883 9 7.2 4
422 1883 10 2.9 4
422 1883 11 -0.2 4
422 1883 12 -1.0 4];
[Yu,~,ix] = unique(ExData(:,2));
Mnthc = accumarray(ix, ExData(:,4), [], @(x){x});
Mnthm = cell2mat(Mnthc.');
figure
plot(1:12, Mnthm)
manudir = {'Jan';'Feb';'Mar';'Apr';'Maí';'Jún';'Júl';'Ágú';'Sep';'Okt';'Nóv';'Des'};
set(gca,'xtick',[1:12],'xticklabel',manudir)
.
  2 commentaires
Haukur Ægir Jóhannesson
This worked great, thank you
Star Strider
Star Strider le 1 Nov 2020
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by