# a file called avehighs.dat stores for three locations

12 次查看（过去 30 天）
Brian2023-3-14

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

### 采纳的回答

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更早的评论隐藏 1更早的评论
Voss 2023-3-14

You're welcome! Any questions, let me know.

### 更多回答（1 个）

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
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

### 类别

Find more on Characters and Strings in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by