Q:

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

0

 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 script, the result will be as shown in the Figure.

Figure Area plot produced from x, y data read as strings from a file 

All Answers

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

Ch12Ex2.m

% Read x and y coordinates of points from a data file

% and plot them using an area plot

x = [];

y = [];

fid = fopen('xandypts.dat');

if fid == -1

 disp('File open not successful')

else

 while ~feof(fid)

 aline = fgetl(fid);

 [letter, rest] = strtok(aline);

 [xval, rest] = strtok(rest);

 [letter, rest] = strtok(rest);

 yval = strtok(rest);

 x = [x str2num(xval)];

 y = [y str2num(yval)];

 end

 area(x,y)

 title(sprintf('%d data points',length(x)))

 

 fclose(fid);

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