help-octave
[Top][All Lists]
Advanced

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

Re: file input question


From: Liam Groener
Subject: Re: file input question
Date: Fri, 5 Feb 2010 20:13:54 -0800


On Feb 5, 2010, at 4:04 PM, Tim Rueth wrote:

I'm brand new to octave, and have a simple question.  I have a file with five numbers per line.  I simply want to read these numbers, one line at a time, into 5 variables.  How do I do this?
 
Also, I tried changing my prompt using PS1 = ..., but octave said that PS1 is no longer a built-in variable.  How do I change my prompt?

There are different ways of doing your file input. See the input/output section of the manual. One way is illustrated with the following script:

clear
fid = fopen('mydata','r'); #Assumes mydata is your file name.
                           #You could interactively input a file name
s=0;
if fid == -1
disp("Couldn't find file mydata")
else
while ~feof(fid) #while not end-of-file
[a,b,c,d,e] = fscanf(fid,'%g %g %g %g %g','C');
# Do some stuff with the variables, .e.g:
s = s+a+b+c+d+e;
end
end
fclose(fid)
#Output something:
s

It is the 'C' in the fscanf call that makes it input one line at a time. In this case, It actually would be more efficient to read the whole file at once into a matrix:
 [v,count]=fscanf(fid,'%g %g %g %g %g',[1,inf]);
 s = sum(v)

As for your second question, setting PS1 in my .octaverc file works for me, Octave-3.2.3. Are you using the development version?


reply via email to

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