Filter löschen
Filter löschen

feof to loop through lines of data?

2 Ansichten (letzte 30 Tage)
David Phung
David Phung am 5 Dez. 2014
Bearbeitet: per isakson am 7 Dez. 2014
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 Kommentare
Image Analyst
Image Analyst am 5 Dez. 2014
OK. Good luck with that. There are examples in the help for fgetl() and sscanf() to help you.
David Phung
David Phung am 6 Dez. 2014
Bearbeitet: Geoff Hayes am 7 Dez. 2014
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

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by