|
From: | financial engineer |
Subject: | RE: reading data using fgets in while loop |
Date: | Mon, 12 Mar 2012 14:19:37 -0400 |
> Date: Mon, 12 Mar 2012 11:14:44 -0700 > From: address@hidden > To: address@hidden > Subject: RE: reading data using fgets in while loop > > > newbie_octave wrote > > > >> Date: Mon, 12 Mar 2012 10:17:32 -0700 > >> From: pr.nienhuis@ > >> To: help-octave@ > >> Subject: Re: reading data using fgets in while loop > >> > >> > >> newbie_octave wrote > >> > > >> > 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! > >> > > >> > I also get the following error in using strsplit, and I don't > >> understand > >> > what mistake I am making as I am following the usage guidelines. I > >> tried > >> > split, but I am advised to use strsplit. > >> > octave-3.2.4:110> strsplit(tline,sep) > >> > error: Invalid call to strsplit. Correct usage is: > >> > > >> > -- Function File: [S] = strsplit (P, SEP, STRIP_EMPTY) > >> > > >> > > >> > Additional help for built-in functions and operators is > >> > available in the on-line version of the manual. Use the command > >> > `doc <topic>' to search the manual index. > >> > > >> > Help and information about Octave is also available on the WWW > >> > at http://www.octave.org and via the help@ > >> > mailing list. > >> > > >> > >> There are a few more text reading functions that may help out. > >> E.g., textread: > >> (applied to your data in a file txt.csv) > >> > >> octave-3.6.1.exe:2> [a, b, c, d] = textread ('tst.csv', "%s %s %f %d", > >> "delimiter", ",") > >> a = > >> { > >> [1,1] = 01/03/2012 > >> [2,1] = 01/04/2012 > >> [3,1] = 01/05/2012 > >> [4,1] = 01/06/2012 > >> [5,1] = 01/09/2012 > >> [6,1] = 01/10/2012 > >> } > >> b = > >> { > >> [1,1] = H (Mar 12) > >> [2,1] = H (Mar 12) > >> [3,1] = H (Mar 12) > >> [4,1] = H (Mar 12) > >> [5,1] = H (Mar 12) > >> [6,1] = H (Mar 12) > >> } > >> c = > >> > >> 26.750 > >> 26.280 > >> 26.000 > >> 25.500 > >> 25.350 > >> 25.050 > >> > >> d = > >> > >> 2584 > >> 2330 > >> 3198 > >> 3045 > >> 2314 > >> 2685 > >> > >> With some postprocessing (e.g., datenum (a, "dd/mm/yyyy") ) you can get > >> any > >> output you want. > >> > >> Philip > >> > >> > >> -- > >> View this message in context: > >> http://octave.1599824.n4.nabble.com/reading-data-using-fgets-in-while-loop-tp4464844p4466694.html > >> Sent from the Octave - General mailing list archive at Nabble.com. > >> _______________________________________________ > >> Help-octave mailing list > >> Help-octave@ > >> https://mailman.cae.wisc.edu/listinfo/help-octave > > > > thanks Philip, > > ...I realize that I don't have textread.m in my io folder. can you tell me > > where can I download it. > > Bobby > > > > What Octave version on what operating system are we talking about? > > I showed how to use textread() with Octave v.3.6.1; in 3.6.0 textread was > significantly improved. > > If your Octave version is not too old (should be >= 3.4.0) you might > download textread.m + the required companion file strread.m here: > http://hg.savannah.gnu.org/hgweb/octave/file/05635e4c605a/scripts/io > and replace the ones on your hard disk in > ./share/octave/<version>/m/io > (You'll have to search a bit where this exactly is, I don't know my way > around on your PC) > > If you run an older Octave version (3.2.4 on Windows?) you're strongly > advised to upgrade a.s.a.p. > > Philip > > -- > View this message in context: http://octave.1599824.n4.nabble.com/reading-data-using-fgets-in-while-loop-tp4464844p4466883.html > Sent from the Octave - General mailing list archive at Nabble.com. > _______________________________________________ > Help-octave mailing list > address@hidden > https://mailman.cae.wisc.edu/listinfo/help-octave hi Philip, yes indeed, my version is older...I am running octave-3.2.4:1> this is the standard package in the applications in ubuntu v.10, and that is what I installed. thanks for that link...it seems like I am missing a few .m running the older version. thanks, Bobby |
[Prev in Thread] | Current Thread | [Next in Thread] |