필터 지우기
필터 지우기

A file called avehighs.dat stores the average high temperatures for each month for a year (rounded to integers). The first column of the data stores the zip code, the 2nd to 13th columns store 12 temperatures.

조회 수: 1 (최근 30일)
Here is the complete question:
A file called avehighs.dat stores the average high temperatures for each month for a year (rounded to integers). The first column of the data stores the zip code, the 2nd to 13thcolumns store 12 temperatures. Write a function that will take in one input argument: the zip code. This function will first read in the data file, then find the line corresponding to the zip code, and plot the temperatures for all 12 months. The axis labels and titles should be as shown. The figure title should show the zip-code.
Here is my code so far:
function output=plottempsWk4_Q6(zipcode)
zipcode=input('Enter one of the following zipcodes(65211,78201,65203,34203): ','s');
load avehighs.dat
avehighs(:,1)=[];
if strcmp(zipcode,65211)==1
x=linspace(1,12);
y=avehighs(1,:);
plot(x,y,'r*')
title('Location 65211')
elseif strcmp(zipcode,78201)==1
x=linspace(1,12);
y=avehighs(2,:);
plot(x,y,'b*')
title('Location 78201')
elseif strcmp(zipcode,65203)==1
x=linspace(1,12);
y=avehighs(3,:);
plot(x,y,'c*')
title('Location 65203')
else strcmp(zipcode,34203)==1
x=linspace(1,12);
y=avehighs(3,:);
plot(x,y,'g*')
title('Location 34203')
end
xlabel('Months')
ylabel('Ave High Temps')
end
i'm not sure what I did wrong here.

채택된 답변

Star Strider
Star Strider 2019년 6월 30일
If you are not getting any output from your function, it is most likely because you did not assign anything to your ‘output’ variable.
If you want to return an handle to the figure your function creates, in your function, you can use:
output = gcf;
Then in your workspace:
figure(output)
to display it (if it’s not already viisible);

추가 답변 (1개)

Guillaume
Guillaume 2019년 6월 30일
편집: Guillaume 2019년 6월 30일
If there was 100 zip codes, would you write a 100 elseif ? What if there was a million? Clearly that's not the way to go
You actually never search the first column for a zip code. There's no need for loops or if to actually find the correct row.
strcmp compare strings or char arrays with strings or char arrays, not with numbers.
I'd recommend using readtable to read your file instead of load. That way you wouldn't even have to get rid of the first row.
Oh, and
if strcmp(a, b) == 1
is better written simply as
if strcmp(a, b)
What you had written convert the true or false result of strcmp to double in order to compare it to 1. The result of the comparison is then the same true or false value returned by strcmp in the first place.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by