Info

This question is closed. Reopen it to edit or answer.

# i want to plot a scatter graph and fit a linear line to it but how do i give the calculated average values as input? can anyone please tell what do i write in place of question marks in the code given?

1 view (last 30 days)
Chitrangini Sahu on 29 Aug 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
daily_temp=xlsread('G:\phd\hydroinformatics\assgn2\Temp_Even_23.5_78.5.xls'); avg_1980=mean(daily_temp(1:60,4)); avg_1981=mean(daily_temp(61:119,4)); avg_1982=mean(daily_temp(120:178,4)); avg_1983=mean(daily_temp(179:237,4)); avg_1984=mean(daily_temp(238:297,4)); avg_1985=mean(daily_temp(298:356,4)); avg_1986=mean(daily_temp(357:415,4)); avg_1987=mean(daily_temp(416:474,4)); avg_1988=mean(daily_temp(475:534,4)); avg_1989=mean(daily_temp(535:593,4)); avg_1990=mean(daily_temp(594:652,4)); avg_1991=mean(daily_temp(653:711,4)); avg_1992=mean(daily_temp(712:771,4)); avg_1993=mean(daily_temp(772:830,4)); avg_1994=mean(daily_temp(831:889,4)); avg_1995=mean(daily_temp(890:948,4)); avg_1996=mean(daily_temp(949:1008,4)); avg_1997=mean(daily_temp(1009:1067,4)); avg_1998=mean(daily_temp(1068:1126,4)); avg_1999=mean(daily_temp(1127:1185,4)); avg_2000=mean(daily_temp(1186:1245,4)); avg_2001=mean(daily_temp(1246:1304,4)); avg_2002=mean(daily_temp(1305:1363,4)); avg_2003=mean(daily_temp(1364:1422,4)); avg_2004=mean(daily_temp(1423:1482,4)); avg_2005=mean(daily_temp(1482:1542,4)); avg_2006=mean(daily_temp(1543:1600,4)); avg_2007=mean(daily_temp(1601:1659,4)); avg_2008=mean(daily_temp(1660:1719,4)); avg_2009=mean(daily_temp(1720:1778,4)); avg_2010=mean(daily_temp(1779:1837,4)); avg_2011=mean(daily_temp(1838:1896,4)); avg_2012=mean(daily_temp(1897:1956,4)); avg_2013=mean(daily_temp(1957:2015,4)); Year=[]; T=[]; for x=1980:2013 Year=[Year,x]; end for y=? T=[T,??]; scatter(Year,?); xlabel('Year'); ylabel('Annual Average Temperature');
##### 1 CommentShowHide None
dpb on 29 Aug 2018
Do NOT create sequentially-named variables like this!!!
Surely the spreadsheet contains a date field in it, doesn't it?
Read it, convert it to datetime and use the features of a timetable to do the summary statistics...particularly retime would be a real time-saver here.

### Answers (1)

Amal George M on 3 Sep 2018
To my understanding, that the intention is to plot average values vs year, and to find a 'linear fit' for the data. For finding linear fit of the data, 'polyfit' function can be used. Here is the modified custom code:
daily_temp=xlsread('G:\phd\hydroinformatics\assgn2\Temp_Even_23.5_78.5.xls');
avg(1)=mean(daily_temp(1:60,4));
avg(2)=mean(daily_temp(61:119,4));
avg(3)=mean(daily_temp(120:178,4));
avg(4)=mean(daily_temp(179:237,4));
avg(5)=mean(daily_temp(238:297,4));
avg(6)=mean(daily_temp(298:356,4));
avg(7)=mean(daily_temp(357:415,4));
avg(8)=mean(daily_temp(416:474,4));
avg(9)=mean(daily_temp(475:534,4));
avg(10)=mean(daily_temp(535:593,4));
avg(11)=mean(daily_temp(594:652,4));
avg(12)=mean(daily_temp(653:711,4));
avg(13)=mean(daily_temp(712:771,4));
avg(14)=mean(daily_temp(772:830,4));
avg(15)=mean(daily_temp(831:889,4));
avg(16)=mean(daily_temp(890:948,4));
avg(17)=mean(daily_temp(949:1008,4));
avg(18)=mean(daily_temp(1009:1067,4));
avg(19)=mean(daily_temp(1068:1126,4));
avg(20)=mean(daily_temp(1127:1185,4));
avg(21)=mean(daily_temp(1186:1245,4));
avg(22)=mean(daily_temp(1246:1304,4));
avg(23)=mean(daily_temp(1305:1363,4));
avg(24)=mean(daily_temp(1364:1422,4));
avg(25)=mean(daily_temp(1423:1482,4));
avg(26)=mean(daily_temp(1482:1542,4));
avg(27)=mean(daily_temp(1543:1600,4));
avg(28)=mean(daily_temp(1601:1659,4));
avg(29)=mean(daily_temp(1660:1719,4));
avg(30)=mean(daily_temp(1720:1778,4));
avg(31)=mean(daily_temp(1779:1837,4));
avg(32)=mean(daily_temp(1838:1896,4));
avg(33)=mean(daily_temp(1897:1956,4));
avg(34)=mean(daily_temp(1957:2015,4));
Year=1980:2013;
scatter(Year,avg);
xlabel('Year'); ylabel('Annual Average Temperature');
linear_coeff = polyval(p,Year,1);
hold on
plot(Year,linear_coeff)
hold off
Side Note: If you are a new user, have a look at the Onramp course.

### Community Treasure Hunt

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

Start Hunting!