?PLEASE READ IN EACH LINE AND USE EVAL ? 12. Write a script that will read from
ID: 3923969 • Letter: #
Question
?PLEASE READ IN EACH LINE AND USE EVAL?
12. Write a script that will read from a file x and y data points in the following format: x 1.3 y 2.2 the letter 'y', space, and the y value. First, create the data file with 10 lines in this format. Do this by using the Editor/Debugger, then File Save As xypts.dat. The script will attempt to open the data file and error-check to make sure it was opened. If so, it uses a for loop and fgetl to read each line as a string. In the loop, it creates x and y vectors for the data points. After the loop, it plots these points and attempts to close the file. The script should print whether or not the file was successfully closed.Explanation / Answer
xyplot.m
fid = fopen('xypts.dat');
if fid == -1
disp('File open not successful')
else
for i = (1:end)
pn(i) = fgetl(fid);
[x rest] = strtok(fid);
[xcoord rest2] = strtok(rest);
[y ycoord] = strtok(rest2);
end
plot(xcoord,ycoord)
close = fclose(fid);
if close == 0
disp('File close unsuccessful')
else
disp('File closed')
end
end
xypts.dat
x 0 y 1
x 1.3 y 2.2
x 2.2 y 6
x 3.4 y 7.4
x 4 y 6.5
x 5 y 2
x 2.2 y 1
x 5 y 9
x 4 y 6.7
x 3.2 y 8.9
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.