Hello again,
In the datafile, I have removed the quotes, replaced the commas with
spaces, and added the matrix dimensions to the second row. The following
program could then create the database, except that the leading zero of
the first variable is lost. The dots in the last column work as "no data".
(Another thing to note is that your data was written between /* and */,
which usually means /*comment*/ and is disregarded. But in this case I'm
not sure if it had any effect.)
/Tom
//File Ex1.csv
//=============================
hs cty_code all_val_mo p1 p2
2 5
0101110000 1220 60600 .5713 .
0101110000 2010 11700 .4286 .
//============================
//Main Program
//=================================================================
#include <oxstd.h>
#import <database>
#include <oxfloat.h>
main()
{
decl file, s, vars = new array[5], hs, cty_code, all_val_mo, p1, p2,
m = new matrix[0][5];
decl dbase = new Database();
file=fopen("Ex1.csv");
fscan(file, "%z", &s); //read first line with variable names
//now scan first line and put them in the
vars array
sscan(s, "%s", &vars[0], "%s", &vars[1], "%s", &vars[2], "%s",
&vars[3], "%s", &vars[4]);
println("\nVariables: ", vars);
fscan(file, "%m", &m); //reads in matrix format - you have to add
the matrix dimensions //before the data
(rows columns)
dbase.Create(1,1,1,rows(m),1); //creates database
dbase.Append(m, vars); //appends data and variable names
p1 = dbase.GetVar("p1");
print("p1:",p1);
println("%v", dbase); //prints database object
}
//=================================================================
Produces the following:
--------------- Ox4 at 11:23:22 on 15-Dec-2006 ---------------
Ox version 4.02 (Windows) (C) J.A. Doornik, 1994-2006
This version may be used for academic research and teaching only
Variables:
[0] = hs
[1] = cty_code
[2] = all_val_mo
[3] = p1
[4] = p2
p1:
0.57130
0.42860
::Database
{
.m_iFreq = 1;
.m_iYear1 = 1;
.m_iPeriod1 = 1;
.m_iYear2 = 2;
.m_iPeriod2 = 1;
.m_mData = <101110000,1220,60600,0.57130000000000003,.;
101110000,2010,11700,0.42859999999999998,. >;
.m_asNames = {"hs","cty_code","all_val_mo","p1","p2"};
.m_mVarsel = <>;
.m_mLagsel = <>;
.m_mSelgroup = <>;
.m_iT1sel = -1;
.m_iT2sel = -1;
.m_iSampleSelMode = 0;
.m_sDbName = ;
.m_vVarType = <0,0,0,0,0 >;
}
//======= End of Output ==========
Christian M. Dahl wrote:
>Dear all,
>
>I am trying to load the variable "p1" from the following csv-file:
>
>// Ex1.csv
>/*=================================================================
>"hs","cty_code","all_val_mo","p1","p2"
>"0101110000","1220",60600, .5713,"."
>"0101110000","2010",11700, .4286,"."
>=================================================================*/
>
>My code is:
>
>//Main Program
>//=================================================================
>#include <oxstd.h>
>#import <database>
>#include <oxfloat.h>
>
>
>main()
>{
> decl dbase = new Database();
>
> dbase.Load("Ex1.csv");
> decl p1 = dbase.GetVar("p1");
> print("p1:",p1);
>}
>//=================================================================
>
>
>Unfortunately, my output is:
>
>//Output
>/*=================================================================
>Ox version 4.04 (Windows) (C) J.A. Doornik, 1994-2006
>This version may be used for academic research and teaching only
>p1:
> 0.00000
> 1.0000
>=================================================================*/
>
>The reported values should have been p1=(.5713,.4286)'.
>
>Any suggestions on how to "correct" this problem would be greatly
>appreciated (I should note that I am working with very large csv files so
>simply modifying the csv file by "hand" is not an option)
>
>Cheers,
>
>Christian M. Dahl
>Associated Prof.
>Purdue University
>
>
>
--
Tom Petersen
Transport and Location Analysis (TLA)
Teknikringen 78 B, 1st floor
KTH, SE-100 44 Stockholm
Phone: +46 (0)8-790 68 33
Fax: +46 (0)8-790 70 02
Mobile: +46 (0)70-424 00 75
Web: http://www.infra.kth.se/~tomp/
|