a file called avehighs.dat stores for three locations
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Voss
on 14 Mar 2023
Edited: Voss
on 14 Mar 2023
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 Comments
More Answers (1)
Adam Drake
on 14 Mar 2023
Edited: Adam Drake
on 14 Mar 2023
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 Comments
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!