[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: problem with fscanf in a loop
From: |
simone marras |
Subject: |
Re: problem with fscanf in a loop |
Date: |
Thu, 20 Aug 2009 22:17:59 +0200 |
Hi James,
thank you very much for helping. It indeed works now with your
suggestion. It just reads them column by column instead of line by
line so that the order is not what expected, but I'll play with it to
fix it. The important is that it finally exits the loop correctly
All the best,
cfd
On Thu, Aug 20, 2009 at 10:04 PM, James Sherman Jr.<address@hidden> wrote:
>> 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.
>
--
Simone Marras, Ph.D. candidate
Barcelona Supercomputing Center
Universitat Politecnica de Catalunya
Avd. Diagonal 647,
Edificio H, planta 10
Barcelona, 08028
SPAIN
Tel.: (+34) 93 4011744
web: www.cranfield.ac.uk/~c086030