How do you get MATLAB to repeat the fgetl or fgets command n times

So my script is this, so far.
fid=fopen('mydata.csv');
a = fgets(fid);
fclose(fid);
This returns the first line of my .csv file.
But what I want is for the script to repeat the command for say the first n rows, then display only those rows. How can I do that?

 Accepted Answer

fid=fopen('mydata.csv');
for K = 1 : n
a = fgets(fid);
fwrite(1, a); %display it
end
fclose(fid);

2 Comments

Thank you very much Mr. Roberson.
But I have to ask.. why in the HECK does this work?
We never defined K. We never defined n. Although I understand that a is in a for loop, I do not understand why it assumes multiple values, because we never related a to K or n.
I am totally confused by this answer..but thanks very much for your help, I appreciate it immensely even if I do not grasp it.
K is defined in the for loop--it takes on the values 1:n in sequence.
doc for % for details
Of course, in the above loop it's only used as the counter against the 'n' limit.
The form as Walter wrote it is a script so all variables are in the workplace. You already have/had a variable n defined or it would have errored--
>> clear n
>> for i=1:n,disp(i),end
Undefined function or variable 'n'.
>>
As for how a is "assuming" multiple values, a is assigned the returned value from the call to fgets on each pass thru the loop--which is the entity actually doing the work of reading a record from the file. After each read, the internal file position is left pointing to the beginning of the next record so the subsequent call picks up from there.
It's time to begin at the beginning... :) Open the doc's and start with the "Getting Started" topic and read thru the tutorials.

Sign in to comment.

More Answers (1)

fid=fopen('mydata.csv');\
s=input('How many lines do you wish to display? ', 's');
N=str2num(s);
for i=1:N
disp(fgetl(fid))
end
fid=fclose(fid);
Add error checking on input string for valid number, etc., etc., ...

6 Comments

Thank you very much dpb. I did not realize that accepting one answer would make be unable to accept a second one.
I tested out your solution and it works fantastically.
I am going to try to add to it to have it write to a variable or file now. Thanks again for your help.
It's simply what fgetl() (*) does--it returns the next line in the file; that's all and it has no additional parameters other than the file handle from which to read that one record. Hence, there's no flexibility built into it; it's a one-note song.
Again, I commend to your attention
doc fgetl % for details
() I used *fgetl instead of fgets that you asked about because of the difference in how they handle the trailing \n -- for viewing I presumed you wouldn't want all the extra blank lines showing up as fgets returns it in the returned string whereas fgetl doesn't. Try replace fgetl w/ fgets to see the difference.
Again, read the documentation.
And, btw, when you look at writing the file you'll want to treat the \n differently in that new file than at the display for viewing--if you use fgetl to not see the blank lines on the screen then you'll need to use a \n in the format string when you write the new file to get the record terminaters therein. Or alternatively if you do use fgets instead, then the \n will be in the string already and you don't need to add it again--and in fact will not want to or you'll do the same thing in the file as the appearance is on the display.
Haha. What have I done today other than read documentation??
I am trying. I dont post questions like this before failing numerous times.
Im almost surprised I didnt get a for loop to work by exhausting the sample space of character combinations :P
Chuckles... :)
Yeah, there is a lot to get a grasp on, granted...
Note,
s=input('How many lines do you wish to display? ')
should be
s=input('How many lines do you wish to display? ', 's');
if you are going to use str2num() on the result.
Yep, good catch...I'll edit it now...intended to avoid the eval() w/ possible unwanted side effects depending on what user happens to input.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!