In Matlab X 0 y 1 x 1.3 y 2.2 x 2.2 y 6 x 3.4 y 7.4 and will create an area plot
ID: 3570192 • Letter: I
Question
In Matlab X 0 y 1 x 1.3 y 2.2 x 2.2 y 6 x 3.4 y 7.4 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, space3the 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. Write a script that will read x and y data points from a file: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
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.