Plotting from an array at an interval

조회 수: 3 (최근 30일)
Haukur Ægir Jóhannesson
Haukur Ægir Jóhannesson 2020년 11월 1일
댓글: Star Strider 2020년 11월 1일
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:

채택된 답변

Star Strider
Star Strider 2020년 11월 1일
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
Haukur Ægir Jóhannesson
Haukur Ægir Jóhannesson 2020년 11월 1일
This worked great, thank you
Star Strider
Star Strider 2020년 11월 1일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by