[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Octave question: Integrating .dat files in functions?
From: |
Joanna Cheng |
Subject: |
Re: Octave question: Integrating .dat files in functions? |
Date: |
Tue, 13 Dec 2011 22:19:09 +1100 |
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;
Hope that helps,
Joanna