help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: problem with fscanf in a loop


From: James Sherman Jr.
Subject: Re: problem with fscanf in a loop
Date: Thu, 20 Aug 2009 16:04:49 -0400

>        while( !feof(file_name_id))
>
>                line_cntr = line_cntr + 1;
>
>                string = fgets(file_name_id);
>                if( strncmp(string,"# curve",7) == 0 )
>                        i = i + 1
>                        [x_curvei(i) y_curvei(i)] = fscanf(file_name_id, "%f 
> %f\n", "C");
>                end
>         end

I'm not familiar with some of the particulars of fscanf and I don't
have octave available currently to test, so I can't say why the fscanf
never exits, but I think I'd rewrite your code a bit different since I
think your logic is not correct.  For example, I think the statement
would only execute once after comment line, then ignore the rest of
the lines till another comment line was reached.  I'd write something
like:

data = [];

while( !feof(file_name_id))

   string = fgets(file_name_id);  % reads in a line
   [tmp_data, n_items] = sscanf(string, "%f %f\n");  % scans the line
for 2 numbers

   if (n_items == 2)      % if 2 numbers were found on the line
      data(end+1, :) = tmp_data;  % add that to the data matrix
   end

end

I'm pretty sure this will work.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]