gdxmrw_intro01_init.gms : Introduction to data transfer between Matlab and GAMS

Description

Initialization: Write "intro1_driver.m" and "link.gms".

This is a simple data transfer example between Matlab and GAMS
by using indexed GDX files. Executing "GDXMRW_Intro01_Init.gms" calls
"intro1_driver.m", that is the driver. The Matlab program writes the GDX
file "inputToGams.gdx" and then calls "link.gms". The GAMS program "link.gms"
manipulates the data values and writes the result into "result.gdx". Finally,
the control is returned to the Matlab program which displays the result.

Intended use: interactive Matlab session

Contributor: Toni Lastusilta


Category : GAMS Data Utilities library


Main file : gdxmrw_intro01_init.gms   includes :  gdxmrw_intro01_init.gms

$Title Introduction to data transfer between Matlab and GAMS

$ontext
Initialization: Write "intro1_driver.m" and "link.gms".

This is a simple data transfer example between Matlab and GAMS
by using indexed GDX files. Executing "GDXMRW_Intro01_Init.gms" calls
"intro1_driver.m", that is the driver. The Matlab program writes the GDX
file "inputToGams.gdx" and then calls "link.gms". The GAMS program "link.gms"
manipulates the data values and writes the result into "result.gdx". Finally,
the control is returned to the Matlab program which displays the result.

Intended use: interactive Matlab session

Contributor: Toni Lastusilta

$offtext


*Write Matlab main program
$onecho > intro1_driver.m
% Data transfer example between Matlab and GAMS.
% A simple example that writes inputToGams.gdx in MatLab, calls GAMS
% program link.gms which manipulates the data values. The result is
% written to result.gdx and displayed in Matlab.
val=1
vec=[1,2,3]
mat=[1,2,3;4,5,6]
iwgdx('inputToGams','val','vec','mat');
system 'gams link.gms';
res=irgdx('result.gdx')
res_val=res.val
res_vec=res.vec
res_mat=res.mat

open intro1_driver.m
$offecho

*Write GAMS program that reads inputToGams.gdx and writes result.gdx
$$onecho > link.gms
set i vector index
    r matrix row
    c matrix column;
Parameter val      Scalar
          vec(i)   Vector
          mat(r,c) Matrix ;

$GDXIN inputToGams
$LOADIDX val vec mat
$GDXIN

Display val,vec,i,mat,r,c;

val      = 2*val;
vec(i)   = 2*vec(i);
mat(r,c) = 2*mat(r,c);

Execute_UnloadIdx 'result.gdx',val,vec,mat;
$$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%;intro1_driver" -nosplash -nodisplay
$endIf