Error reading Two dimensional Array from Excel

310 views
Skip to first unread message

vidit

unread,
Apr 16, 2009, 11:37:48 AM4/16/09
to AMPL Modeling Language
hi,
I want to read a two dimensional array from Excel

destination 1 2 3 4 5
NY 0 0 1 0 0
MD 3 0 0 0 0
TX 0 2 0 2 0
Where destination is a set and set times consist of 1 2 3 4 5 .

I tried using the code given in the book and modified mine. I used
this code to extract the data

table fleetdemand IN "ODBC" "travel.xls" "demand": [i ~ destination],
{j in times} <dem[i,j] ~ (j)>;

I have the following error while reading,(here i have 32 columns
instead of 5):
Error reading table fleetdemand with table handler odbc:
Column "1" does not appear in table demand of "travel.xls",
which has 32 columns:

Col Name

10 "F10"
11 "F11"
12 "F12"
13 "F13"
14 "F14"
15 "F15"
16 "F16"
17 "F17"
18 "F18"
19 "F19"
2 "F2"
20 "F20"
21 "F21"
22 "F22"
23 "F23"
24 "F24"
25 "F25"
26 "F26"
27 "F27"
28 "F28"
29 "F29"
3 "F3"
30 "F30"
31 "F31"
32 "F32"
4 "F4"
5 "F5"
6 "F6"
7 "F7"
8 "F8"
9 "F9"
1 "destination"
Can anyone help where i am going wrong? I appreciate your help.

Thanks,

Vidit

Robert Fourer

unread,
Apr 17, 2009, 10:23:45 AM4/17/09
to am...@googlegroups.com, vidit

Enter the table column headings into the spreadsheet as strings rather than
numbers: '1 rather than 1, etc. Be sure that NY, MD, TX do not start with a
space. Then it works like this (except you would use model and data files
instead of typing everything at the command prompt):

ampl: set destination;
ampl: set times;
ampl: param dem {destination,times};

ampl: data;
ampl data: set destination := NY MD TX ;
ampl data: set times := 1 2 3 4 5 ;
ampl data: model;

ampl: table fleetdemand IN "ODBC" "travel.xls" "demand":
ampl? [i ~ destination], {j in times} <dem[i,j] ~ (j)>;
ampl: read table fleetdemand;

ampl: option display_1col 0;
ampl: display dem;
dem [*,*] (tr)
: MD NY TX :=
1 3 0 0
2 0 0 2
3 0 1 0
4 0 0 2
5 0 0 0
;

Note that I defined the range "demand" to encompass only the four rows and
six columns of the spreadsheet that contained the table.

Bob Fourer
4...@ampl.com

Reply all
Reply to author
Forward
0 new messages