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 Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by