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

Write a script that will read x and y data points from a file, and will create a

ID: 3773799 • Letter: W

Question

Write a script that will read x and y data points from a file, and will create an area

plot with those points. The format of every line in the file is the letter ‘x’, a space,

the x value, space, the letter ‘y’, space, and the y value. You must assume that the

data file is in exactly that format, but you may not assume that the number of lines

in the file is known. The number of points will be in the plot title. The script loops

until the end of file is reached, using fgetl to read each line as a string. For

example, if the file contains the following lines

x 0 y 1

x 1.3 y 2.2

x 2.2 y 6

x 3.4 y 7.4

when running ,the result will be as shown in Figure

Explanation / Answer

fid = fopen('zypoints.dat');
if fid == -1
   disp('File open not successful')
else
   %Initialize z vector and y vector
   zvec = 1:10;
   yvec = 1:10;
   for i = 1:10
       aline = fgetl(fid);
       %Separate each line into two parts: z and y
       [z rest] = strtok(aline,'y');
       z(1:2) = []; %Removes the "z" and the space
       [let y] = strtok(rest);
       zvec(i) = str2num(z);
       yvec(i) = str2num(y);
   end
   plot(zvec,yvec,'ko')
   xlabel('z')
   ylabel('y')
   title('Points from file "zypoints.dat"')
   %Error-check file close closeresult = fclose(fid);
   if closeresult == 0
       disp('File close successful')
   else
       disp('File close not successful')
   end
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