[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
reading data using fgets in while loop
From: |
newbie_octave |
Subject: |
reading data using fgets in while loop |
Date: |
Sun, 11 Mar 2012 16:00:36 -0700 (PDT) |
I have a .csv file with the following data
01/03/2012,H (Mar 12),26.750000,2584
01/04/2012,H (Mar 12),26.280000,2330
01/05/2012,H (Mar 12),26.000000,3198
01/06/2012,H (Mar 12),25.500000,3045
01/09/2012,H (Mar 12),25.350000,2314
01/10/2012,H (Mar 12),25.050000,2685
and I am a newbie to octave. I ran the following command to read the above
data into octave
X=csvread("/tmp/hist.csv")
but it returns the following matrix.
1.0000e+00 0.0000e+00 2.6750e+01 2.5840e+03
1.0000e+00 0.0000e+00 2.6280e+01 2.3300e+03
1.0000e+00 0.0000e+00 2.6000e+01 3.1980e+03
1.0000e+00 0.0000e+00 2.5500e+01 3.0450e+03
1.0000e+00 0.0000e+00 2.5350e+01 2.3140e+03
1.0000e+00 0.0000e+00 2.5050e+01 2.6850e+03
Obviously, it is not reading the text correctly
so I am now using fgets() as follows:
fid=fopen(fname);
fout="out.mat"
global tline;
global tempstr;
while 1
tline = fgets(fid);
sep=",";
tempstr=strsplit(tline, sep);
dt=tempstr(1);
cname=tempstr(2);
price=str2double(tempstr(3));
volume=str2double(tempstr(4));
if ~ischar(tline), break,end;
end
disp(tline);
fclose(fid);
but when I run the script, it returns -1 due to disp(tline) being after the
end. I want to be able to compute the volume-weighted average price, and not
have to do it all within the script that reads the data from the file. But,
given that I cannot access any of the variables outside the while loop, I am
stuck. Ideally, I could have used csvread() or dlmread() but that is messing
up the string fields in my file. Can anyone please suggest how I fix this so
I can manipulate the variables outside the while loop that gets the data
from the .csv file. thanks!
--
View this message in context:
http://octave.1599824.n4.nabble.com/reading-data-using-fgets-in-while-loop-tp4464844p4464844.html
Sent from the Octave - General mailing list archive at Nabble.com.
- reading data using fgets in while loop,
newbie_octave <=
- Re: reading data using fgets in while loop, indium, 2012/03/12
- Re: reading data using fgets in while loop, indium, 2012/03/12
- RE: reading data using fgets in while loop, financial engineer, 2012/03/12
- Re: reading data using fgets in while loop, indium, 2012/03/12
- RE: reading data using fgets in while loop, financial engineer, 2012/03/12
- Re: reading data using fgets in while loop, indium, 2012/03/12
- RE: reading data using fgets in while loop, financial engineer, 2012/03/12
- Re: reading data using fgets in while loop, indium, 2012/03/13
- RE: reading data using fgets in while loop, financial engineer, 2012/03/13
Re: reading data using fgets in while loop, PhilipNienhuis, 2012/03/12