Effacer les filtres
Effacer les filtres

feof to loop through lines of data?

2 vues (au cours des 30 derniers jours)
David Phung
David Phung le 5 Déc 2014
Modifié(e) : per isakson le 7 Déc 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 commentaires
Image Analyst
Image Analyst le 5 Déc 2014
OK. Good luck with that. There are examples in the help for fgetl() and sscanf() to help you.
David Phung
David Phung le 6 Déc 2014
Modifié(e) : Geoff Hayes le 7 Déc 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

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by