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 from a file x and y data points in the following f

ID: 3654623 • Letter: W

Question

Write a script that will read from a file x and y data points in the following format: 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. First, create the data file with 10 lines in this format. Do this by using the Editor/Debugger, and then File Save As xypts.dat. The script will attempt to open the data file and error-check to make sure that 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 the file was successfully closed.

Explanation / Answer

[fid,msg]=fopen('xypts.dat', 'r'); if (strcmpi(msg,'') ==0) fprintf('File ''xypts.dat'' fail to open '); else fprintf('File ''xypts.dat'' opened successfully '); for i=1:10 tline=fgetl(fid); a=textscan(tline,'%c %f %c %f'); x(i)=a{2}; y(i)=a{4}; end plot(x,y,'*-'); %close file close=fclose(fid);%0 success, -1 failed if(close~=0) fprintf('File ''xypts.dat'' close failed '); else fprintf('File ''xypts.dat'' closed successfully '); 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