Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function to read/display/process the state historic economic data. A sam

ID: 3655577 • Letter: W

Question

Write a function to read/display/process the state historic economic data. A sample data is attached. The format of the data is: The 1st line contains the state name, for example, STATE: Missouri The 2nd line contains the header for the for data columns: YEAR Population income_nonfarm income_farm From the 3rd line: 4 columns of data. Please note that the income data has the unit of 1000$. Requirements for the Matlab program: (1) The function should have the file path of the data file as the single input argument, and no output argument; (2) Read the name of the state from the data file; (3) Read all four columns of data; (4) Calculate the per capita income for each year in the data file. The calculation equation is: Per capita income = (income_nofarm + income_farm)

Explanation / Answer

function ReadHistoricEconData(path) close all; [fid,msg] = fopen(path, 'r'); if (strcmpi(msg,'') ==0) fprintf('File ''%s'' fail to open ',path); else fprintf('File ''%s'' opened successfully ',path); state=fgetl(fid); %state is first line tline=fgetl(fid); header=textscan(tline,'%s %s %s %s'); %header is second line dat=textscan(fid,'%d %f %f %f'); years=dat{1}; pop=dat{2}; nonfarm=dat{3}; farm=dat{4}; % Per capita income = (income_nofarm + income_farm)÷population perCapita=zeros(1,length(years)); %for speed, predefine size for i=1:length(years) perCapita(i)=(nonfarm(i)+farm(i))/pop(i); end h=figure; str=sprintf('Historic Data for %s ',state); set(h,'name',str,'numbertitle','off') subplot(2,1,1); loglog(years,pop,'LineWidth',2,'color',[0.64 0.16 0.16]); set(gca,'fontsize',14); xh=xlabel(header{1}); yh=ylabel(header{2}); set([xh,yh],'fontweight','bold'); title('(Population vs. Year)'); subplot(2,1,2); plot(years,perCapita*1000,'b','LineWidth',2); set(gca,'fontsize',14); xh=xlabel(header{1}); yh=ylabel('Per Capita Income'); set([xh,yh],'fontweight','bold'); title('Per Capita Income vs.Year'); %close file closed=fclose(fid);%0 success, -1 failed if(closed~=0) fprintf('File ''%s'' close failed ',path); else fprintf('File ''%s'' closed successfully ',path); end k=strfind(path,''); if(length(k)~=0) newpath=strcat(path(1:k(end)),'new_',path(k(end)+1:end)); else newpath=strcat('new_',path); end fid=fopen(newpath,'w'); for i=1:length(years) fprintf(fid,'%d %f ',years(i),perCapita(i)); end fclose(fid); end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote