Description
Initialization: Write "sample.gdx" and "rgdx01.m". The program demonstrates reading a GDX file into a structure in Matlab by using RGDX. Intended use: interactive Matlab session Contributor: Toni Lastusilta
Category : GAMS Data Utilities library
Main file : gdxmrw_rgdx01_init.gms includes : gdxmrw_rgdx01_init.gms
$Title Reading data from a GDX file into a structure with RGDX
$ontext
Initialization: Write "sample.gdx" and "rgdx01.m".
The program demonstrates reading a GDX file into a structure in
Matlab by using RGDX.
Intended use: interactive Matlab session
Contributor: Toni Lastusilta
$offtext
*Write sample.gdx
Set i Domain set i /1*4/
j Domain set j /j1*j2/
k Domain set k /k1*k2/
l(i,j) "Dynamic subset l(i,j)" /#i.#j/
m(i,j,k) "Dynamic subset m(i,j,k)" /#i.#j.#k/;
Alias(i,a);
Parameter test2(i,j);
test2(i,j)= ord(i)+2*ord(j);
test2('1','j1')=no;
Parameter test3(i,j,k);
test3(i,j,k)=ord(i)+ord(j)+ord(k);
Set plants canning plants / seattle 'Washington'
san-diego 'California' /
markets end-markets / new-york 'New York'
chicago 'Illinois'
topeka 'Kansas' /;
Positive Variable ship(plants,markets) shipment quantities ;
*Set variable attributes lower and upper bound, as well as, marginal
ship.lo(plants,markets)= 5;
ship.up(plants,markets)=1000;
ship.m(plants,markets)=10;
Execute_unload 'sample.gdx';
*Display GDX file content
$set displayGDX gdxdump
$if not %system.filesys%==UNIX $if %GAMS.ide%==1 $set displayGDX gamside
Execute '%displayGDX% sample.gdx';
*Write Matlab program
$onecho > rgdx01.m
% Using RGDX to read a GDX file into a structure.
% E.g. alphabetic set labels, variable levels and bounds.
% INPUT STRUCTURE before reading the GDX file
% mandatory field: name
% optional fields: form, compress, uels, field, ts, te
% OUTPUT STRUCTURE after the GDX file is read
% fields: name, type, val, uels, form, dim, field, ts, te
% INPUT STRUCTURE
s1.name = 'test3'; %symbol name
s1.form = 'full'; %full/sparse
s1.compress = true; %true/false
% Read structure s1: field name(symbol) is test3 from sample.gdx
x1 = rgdx('sample', s1) %Matlab structure
x1.val %value
x1.uels{1} %UEL(Unique Element Label)
x1.uels{2} %display the cell array content for dim=2
x1.uels{3}
%Define function: Generate a Unique Element Labels listing (gUEL)
guel = @(s,v) strcat(s,strsplit(num2str(v)));
% Use a filterad read in order to get only specific UEL:s
s2.name = 'test3';
s2.form = 'full';
s2.compress = false; %filtered read requires that compression=false
s2.uels = {guel('',[1,3]),guel('j',1:2),guel('k',1:2)};
x2 = rgdx('sample',s2)
x2_val=x2.val
% OUTPUT STRUCTURE
s3.name = 'test3'; %symbol name
x3 = rgdx('sample',s3) %Matlab structure
x3_val=x3.val %value
x3_form=x3.form %full/sparse
x3_type=x3.type %set/parameter/variable/equation
x3_uels=x3.uels %Unique Element Labels
x3_dim=x3.dim %dimension
x3_uels_dim1=x3.uels{1} %Unique Element Labels for dimension 1
% Read lower and upper bound of variable ship from sample.gdx
s4.name = 'ship';
s4.field = 'lo'; %field=l/m/up/lo/s
x4_lo= rgdx('sample',s4); %Read lower bound values
s4.field= 'up';
x4_up= rgdx('sample',s4); %Read upper bound values
x4_lo_val=x4_lo.val
x4_up_val=x4_up.val
% Read set plants and display symbol and element text
s5.name= 'plants'
s5.ts= true;
s5.te= true;
x5= rgdx('sample',s5)
uels= x5.uels{1}
x5_ts= x5.ts %Text for Symbol
x5_te =x5.te %Text for Symbol Element
open rgdx01.m
$offecho
*Call Matlab
$set WHICH which
$if not %system.filesys% == UNIX $set WHICH where
$call %WHICH% matlab
$ifThen errorLevel 1
$clearError
$abort.noerror 'Matlab is not available!';
$else
$call.Async matlab -r "cd %GAMS.cdir%;rgdx01" -nosplash -nodisplay
$endIf