feof to loop through lines of data?

2 次查看(过去 30 天)
Hey, so i'm supposed to have a file called xypts with data like below where everything is spaced one apart.
z 0 y 1
z 4.6 y 2.2
z 6.8 y 6
z 8.1 y 7.4
I have to use a while loop and fgetl to read each line as a string. In the loop, it creates z and y vectors for the data points. It will loop until the end of the file is reached. The number of points should be in the plot title.
  2 个评论
Image Analyst
Image Analyst 2014-12-5
OK. Good luck with that. There are examples in the help for fgetl() and sscanf() to help you.
David Phung
David Phung 2014-12-6
编辑:Geoff Hayes 2014-12-7
I must've deleted the rest of my question on accident... but I was wondering why my feof was only giving me one data point, and then I figured it out. I just had to add a counter, n.
Here is my code:
clear all; clc
fid=fopen('xypts.csv');
n=1;
if fid== -1
disp('File open not successful')
else
while feof(fid)==0
aline=fgetl(fid);
string = strsplit(aline);
zvec(n)=(string(2));
yvec(n)=(string(4));
n=n+1;
end
z=str2double(zvec);
y=str2double(yvec);
zlength=length(z);
plot(z,y,'*')
plottitle=sprintf('%d number of points',zlength);
title=(plottitle)
close=fclose(fid);
if close == -1
disp('File close not successful')
else
disp('File close successful')
end
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by