Q:

Create a data file to store information on hurricanes. Each line in the file should have the name of the hurricane, its speed in miles per hour, and the diameter of its eye in miles

0

 Create a data file to store information on hurricanes. Each line in the file should have the name of the hurricane, its speed in miles per hour, and the diameter of its eye in miles. Then, write a script to read this information from the file and create a vector of structures to store it. Print the name and area of the eye for each hurricane. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Ch9Ex18.m

% Reads hurricane information and store in vector

% of structures, print name and area for each

fid = fopen('hurricane.dat');

if fid == -1

 disp('File open not successful')

else

 i = 0;

 while feof(fid) == 0

 i = i + 1;

 aline = fgetl(fid);

 [hname, rest] = strtok(aline);

 [speed, diam] = strtok(rest);

 hstruc = struct('Name', hname, 'Speed', ...

 str2num(speed), 'Diam', str2num(diam));

 hurricane(i) = hstruc;

 end

 for i = 1:length(hurricane)

 fprintf('%s had area %.2f\n', hurricane(i).Name, ...

 pi * (hurricane(i).Diam/2)^2)

 end

 

 closeresult = fclose(fid);

 if closeresult == 0

 disp('File close successful')

 else

 disp('File close not successful')

 end 

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now