feof to loop through lines of data?

조회 수: 4 (최근 30일)
David Phung
David Phung 2014년 12월 5일
편집: per isakson 2014년 12월 7일
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에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by