Effacer les filtres
Effacer les filtres

a file called avehighs.dat stores for three locations

1 vue (au cours des 30 derniers jours)
Brian
Brian le 14 Mar 2023
Modifié(e) : Voss le 15 Mar 2023
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

Réponse acceptée

Voss
Voss le 14 Mar 2023
Modifié(e) : Voss le 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 commentaires
Brian
Brian le 14 Mar 2023
Thank you so much!
Voss
Voss le 14 Mar 2023
Modifié(e) : Voss le 15 Mar 2023
You're welcome! Any questions, let me know.

Connectez-vous pour commenter.

Plus de réponses (1)

Adam Drake
Adam Drake le 14 Mar 2023
Modifié(e) : Adam Drake le 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

Catégories

En savoir plus sur MATLAB Report Generator dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by