a file called avehighs.dat stores for three locations

1 visualización (últimos 30 días)
Brian
Brian el 14 de Mzo. de 2023
Editada: Voss el 15 de Mzo. de 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

Respuesta aceptada

Voss
Voss el 14 de Mzo. de 2023
Editada: Voss el 14 de Mzo. de 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 comentarios
Brian
Brian el 14 de Mzo. de 2023
Thank you so much!
Voss
Voss el 14 de Mzo. de 2023
Editada: Voss el 15 de Mzo. de 2023
You're welcome! Any questions, let me know.

Iniciar sesión para comentar.

Más respuestas (1)

Adam Drake
Adam Drake el 14 de Mzo. de 2023
Editada: Adam Drake el 14 de Mzo. de 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

Categorías

Más información sobre Low-Level File I/O en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by