help-octave
[Top][All Lists]
Advanced

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

Re: Octave question: Integrating .dat files in functions?


From: John W. Eaton
Subject: Re: Octave question: Integrating .dat files in functions?
Date: Tue, 13 Dec 2011 10:57:27 -0500

On 13-Dec-2011, Joanna Cheng wrote:

| On Tue, Dec 13, 2011 at 8:40 PM, StudentinCrisis <address@hidden> wrote:
| > I've also tried this code but it doesn't work:
| >
| > fid = fopen('wierd.dat','r');
| > numInts = fscanf(fid, '%d', 1);
| > for(i = 1: numInts)
| > x = fscanf(fid, '%d', 1);
| > sum = sum + x;
| > end
| > sum
| >
| > it says "error: Invalid call to sum."
| 
| The return value from fscanf is the first number read, not the total
| number of entries.
| "sum" is an octave function, that's why you're getting that error.
| 
| Try this:
| 
| fid = fopen("weird.dat", "r");
| total = 0;
| k = fscanf(fid, '%d', 1);
| while (!isempty(k))
|    total += k;
|    k = fscanf(fid, '%d', 1);
| endwhile;

Or this (if I understand correctly, the first number in the file is
supposed to be how many more to read):

  fid = fopen ('wierd.dat', 'r');
  num_ints = fscanf (fid, '%d', 1);
  x = fscanf (fid, '%d', num_ints);
  fclose (fid);
  sum (x)

jwe


reply via email to

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