a file called avehighs.dat stores for three locations
3 次查看(过去 30 天)
显示 更早的评论
Full question:
A file called avehighs.dat stores for three locations the average high temperatures for each month for a year (rounded to integers). There are three lines in the file; each stores the location number followed by the 12 temperatures (this format may be assumed). For example, the file might store:
432 33 37 42 45 53 72 82 79 66 55 46 41
777 29 33 41 46 52 66 77 88 68 55 48 39
567 55 62 68 72 75 79 83 89 85 80 77 65
Write a script that will read these data in and plot the temperatures for the three locations separately in one Figure Window. A for loop must be used to accomplish this.
My code so far:
avehighs=[432 33 37 42 45 53 72 82 79 66 55 46 41;777 29 33 41 46 52 66 77 88 68 55 48 39;567 55 62 68 72 75 79 83 89 85 80 77 65];
save avehighs.dat avehighs -ascii
[a,b,c]=plottemp(avehighs)
function [h,j,k]=plottemp(d)
y=d(1,2:13);
for i=1:12
x=i;
h=plot(x,y,'bo');
xlabel('Month')
ylabel('Average High Temps')
title('Location',d(2,1))
end
y2=d(2,2:13);
for i=1:12
x=i;
j=plot(x,y2,'bo');
xlabel('Month')
ylabel('Average High Temps')
title('Location',d(2,1))
end
y3=d(3,2:13);
for i=1:12
x=i;
k=plot(x,y3,'bo');
xlabel('Month')
ylabel('Average High Temps')
title('Location',d(3,1))
end
end
0 个评论
采纳的回答
Voss
2023-3-14
编辑:Voss
2023-3-14
Instead of looping over months, (for i = 1:12), you can plot all the months for a given location at once, and loop over locations (for jj = 1:size(d,1)).
Something like this:
avehighs=[432 33 37 42 45 53 72 82 79 66 55 46 41;777 29 33 41 46 52 66 77 88 68 55 48 39;567 55 62 68 72 75 79 83 89 85 80 77 65];
save avehighs.dat avehighs -ascii
h = plottemp(avehighs);
function h = plottemp(d)
hold on
x = 1:12;
h = zeros(1,size(d,1));
for jj = 1:size(d,1)
y = d(jj,2:13);
h(jj) = plot(x,y,'o');
end
xlabel('Month')
ylabel('Average High Temps')
legend(compose('Location %d',d(:,1)),'Location','best')
end
2 个评论
更多回答(1 个)
Adam Drake
2023-3-14
编辑:Adam Drake
2023-3-14
clc, clear variables, close all
avehighs=[432 33 37 42 45 53 72 82 79 66 55 46 41;...
777 29 33 41 46 52 66 77 88 68 55 48 39;...
567 55 62 68 72 75 79 83 89 85 80 77 65];
save avehighs.dat avehighs -ascii
input = readmatrix('avehighs.dat');
plottemp(input)
function plottemp(data)
month = 1:12;
location = data(:,1);
temp = data(:,2:13);
plot(month,temp)
xlabel('Month')
ylabel('Average High Temp')
title('Average High Temperatures')
legend(num2str(location),'Location','South')
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Report Generator 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!