Ap A p p l i c ati at i o n Not No t e – Call Cal l i n g CST Stu d i o f r o m Matl Mat l ab Abstract .............................................. ..................................................................... .............................................. .............................................. .......................................... ................... 1 1. Launch CST from fr om Matlab Ma tlab command line l ine............................................ ................................................................... ................................... ............ 2 1.1. Example 1: launch CST, open an existing model, solve, save ........................................ ........................................ 2 2. Launch and control CST from a Matlab .m file ............................................... ................................................................... .................... 2 2.1. Starting CST DESIGN ENVIRONMENT and opening an MWS window .................... 3 2.2. Translating CST-specific VBA commands into Matlab ................................................. ................................................. 3 2.3. Useful commands – some examples ..................................................... ............................................................................. .......................... .. 4 2.4. Example 1 again: launch CST, open an existing existi ng model, solve, save .............................. .............................. 5 2.5. Example 2: Open an MWS file, perform a parameter study, store a result in a Matlab vector ........................................... .................................................................. .............................................. .............................................. ............................................. ...................... 5
Ab A b s t r act ac t This application note gives background information on linking CST Studio to Matlab, by calling CST directly from Matlab.
For information on how to call Matlab from within CST Studio, please see the separate Application Note CST_USER_NOTE__MATLAB_COM_DDE.p CST_USER_NOTE__MATLAB_COM_DDE.pdf df .
Methods There are two main ways to call CST Studio from within Matlab: 1) launch CST from Matlab command line and use a separate VBA file to instruct CST what operations to do 2) run a Matlab .m file containing CST Studio Studio commands
1. Launch CST from fro m Matlab Matlab comm c ommand and li ne This is the simplest way to run CST from within Matlab. It is appropriate when the CST model name and operations to be performed are known in advance and do not need to change.
At the Matlab command line, enter:
>> ! " CST_DS_Path\ CST DESI DESI GN ENV ENVI RONMENT. ENT. exe" – m cmdfile. bas where CST_DS_Path is the path to the CST STUDIO SUITE installation directory, and cmdfile. bas is a Basic (VBA) file containing the commands to be executed within CST STUDIO SUITE.
1.1. 1.1. Exampl Exampl e 1: 1: laun ch CST, CST, open an exist ing mod el, solv e, save For example, when CST_DS_Path is Pr ogr ogr amFi l es\ CSTST STSTU UDI O SUI SUI TE 200 2006 6 and the command file name (including path) is C: \ Wor k\ St ar t _CS _CST. bas the Matlab command line would look like follows: >> ! " C: \ Pr ogr ogr amFi l es\ CSTST STSTU UDI O SUI SUI TE ENVI RONMENT. exe" exe" –m C: \ Wor k\ St ar t _CS _CST. bas
2006 2006\\ CST
DESI ESI GN
The contents of the St ar t _CS _CST. bas file could look as follows:
Sub Main OpenFile("C:\work\test1.cst”) Solver.Start Save End Sub This simple VBA script opens the CST model called test1.cst, starts the solver, saves the model and then exits.
2. Launch and contro con troll CST CST from fro m a Matlab Matlab .m file In this case, all the commands that need to be executed in CST, instead of being contained in a VBA .bas file are contained contained in a Matlab .m file. They could as well be entered directly from the Matlab prompt. This technique has the advantage that it offers complete control of CST MWS from within Matlab. Various operations, like solving several models, performing parameter studies, changing a model, saving or discarding MWS results, etc. are therefore accessible.
The user has access to all CST-specific VBA commands, by means of the Matlab command i nvoke voke.
2.1. 2.1. Start ing in g CST DESIGN DESIGN ENVI ENVIRON RONMEN MENT T and op enin g an MWS window The first step is typically starting the CST DESIGN ENVIRONMENT . To perform this operation, the following command syntax is used: cst = act xser ser ver ( ' CSTSt udi o. appl i cat cat i on' ) “ c s t ” can ” can be replaced by any other variable name. To open a new MWS window in CST DS, the following syntax is used: mws = i nvoke( nvoke( cs t , ' NewMWS' ) where “ mws ” can be replaced by any other variable name.
To open an existing MWS model , you can (just as if you were working interactively within CST DS): - open the model directly into CST DS: mws = i nvoke( cst, ' OpenFi l e' , ‘ f i l ename. cst' ) ; - or, first open an empty MWS window, then open the model in this window: mws = i nvoke( nvoke( cs t , ' NewMWS' ) i nvoke( mws, ' OpenFi l e' , ‘ f i l ename. cst' ) ;
2.2. 2.2. Translating Translating CSTCST-specifi specifi c VBA comm ands i nto Matlab Matlab Any set of CST-specific VBA commands can be issued directly from Matlab. For example, the VBA code for the generation of a brick: With Brick .Reset .Name “Brick1” .Component “component1” “component1” .Xrange “0”, “1” .Yrange “0”, “1” .Zrange “0”, “1” .Material “Vacuum” .Create End With
translates into the following Matlab code:
br i ck = i nvoke ke(( mws, % cr ea eatt e a br i i nvoke( br i c k, i nvoke( br i c k, k, i nvoke voke(( br i ck, ck, i nvoke( br i c k, i nvoke( br i c k, i nvoke( br i c k, i nvoke( br i c k, k, i nvoke( br i c k,
' Br i ck' ck' ) ; ck i n MWS ' Res et et ' ) ; ' Name' , ' Br i c k1 k1' ) ; ' Component ' , ' com component 1' ) ; ' Xr an a nge' , ' 0' , ' 1' ) ; ' Yr an a nge' , ' 0' , ' 1' ) ; ' Zr an a nge' , ' 0' , ' 1' ) ; ' Mat er i al ' , ' Vac uu uum' ) ; ' Cr eat e' ) ;
% . . . pos s i bl y cr cr eat e ot her br i c ks ks he her e r el eas e( e( br i c k) ; Note: if several bricks need to be created, the pair of commands br i ck = i nvoke( voke( mws, ' Br i c k' ) and r el ease( se( br i ck) ck) only need to be issued once.
2.3. 2.3. Useful Useful comm ands – s ome examples Find out the application name and version vers ion app app = i nvoke( voke( mws, ' Get Appl i cat i onN onName' ) ver ver = i nvoke voke(( mws, ' Get Appl i cat cat i onVer si on' ) Set the units uni t s = i nvoke ke(( mws, ' Uni t s' ) ; i nvoke ke(( uni t s, ' Geomet r y' , ' mm' ) ; i nvoke( uni t s , ' Fr equenc y' y' , ' ghz ' ) ; i nvoke( uni t s , ' Ti me' , ' s ' ) ; Set the value of a variable l en1 en1=40. 40. 00; i nvoke ke(( mws, ' St or ePar amet er ' , ' l en1' , l en1) ; % “mws” i s t he Mat l ab nam name of t he MWS obj obj ect , see &2. &2. 1 Solver commands examples sol sol ver = i nvoke ke(( mws, ' Sol ver ' ) ; i nvoke( sol sol ver , ' Fr equencyR cyRange' , f r 1, f r 2) ; f r equ equency ency r ang ange i nvoke( s ol ol ver , ' St ar t ' ) ; Working with 1D results r esul sul t = i nvoke ke(( mws, ' Resul sul t 1D' , ' a1( 1) 1( 1) ' ) ;
numOf Val ue = i nvoke voke(( r esul sul t , ' Get N' ) ;
% Set
t he
% Exampl e f or % S11 l i near % Mi ght be used t o % i ni t i al i z e var i abl es
i nvoke( r es ul ul t , ' Save' , ' C: \ t mp\ f i l ename' ) ; A = i mpor t dat a( ' C: \ t mp\ f i l ename' , ' ' , 4) x = A. dat a( : , 1) ; % x- dat a col col umn y = A. dat a( : , 2) ; % y- dat a col col umn
2.4. 2.4. Exampl Exampl e 1 again: l aunch aunc h CST, CST, open an exis ting ti ng m odel, so lve, save To reach the same effect as in the example shown in paragraph 1.1, the following set of commands need to be entered into the .m file: cst = act xser ser ver ( ' CSTSt udi o. appl i cat cat i on' ) mws = i nvoke( nvoke( cs t , ' NewMWS' ) i nvoke( mws , ' OpenFi l e' , ' C: \ wor k\ t es t 1. c st st ' ) ; sol sol ver = i nvoke ke(( mws, ' Sol ver ' ) ; i nvoke( s ol ol ver , ' St ar t ' ) ; i nvoke voke(( mws, ' Save' ve' ) ; r el eas e( e( s ol ol ver ) ; r el ease( mws) ; r el eas e( e( c s t ) ;
2.5. 2.5. Examp Example le 2: Open Open an MWS MWS file, perfo rm a parameter st udy, ud y, sto re a result in a Matlab Matlab vector The following code can be used to perform this operations. (The code is contained in the attached file studio3.m . It refers to the CST model tsplitter.cst.) %mat l abab- exam exampl e: t est ed 0909- J an 2008 2008 wi t h wi t h MATLAB TLAB 7. 5 ( R2007 2007b) b) % Open an MWS f i l e, per f or m a par par amet er st udy, % st or e a r esul sul t i n a Mat l ab vect or % To t es e s t , f i r s t c opy t he f i l e " t s pl pl i t t er . c s t " % i n t he di r ectory "C: "C: \ CST_Te _Test " % St ar t CST St udi o cst = act xser ser ver ( ' CSTSt udi o. appl i cat cat i on' ) mws = i nvoke( nvoke( cs t , ' NewMWS' ) app app = i nvoke( voke( mws, ' Get Appl i cat i onN onName' ) ver ver = i nvoke voke(( mws, ' Get Appl i cat cat i onVer si on' ) % Open t he MWS model i nvoke( mws , ' OpenFi l e' , ' C: \ CST_ Te Tes t \ t s pl pl i t t er . c st st ' ) ; f or i i i = 1: 1: 2 % Par amet er sweep % Set val ue of par amet er i nvoke( mws , ' St or ePar amet er ' , ' of f s et et ' , i i i ) ; i nvoke ke(( mws, ' Rebui l d' ) ; % St ar t s ol ol ver sol sol ver = i nvoke ke(( mws, ' Sol ver ' ) ; i nvoke( s ol ol ver , ' St ar t ' ) ; % Take over ver t he r esul sul t ; her e, as an exam xampl e, % t he ampl i t ude ( l i near ) of S1 S11 r esul sul t = i nvoke( mws, ' Resul sul t 1D' , ' a1( 1) 1( 1) ' ) ;
numOf Val ue = i nvoke voke(( r esul sul t , ' Get N' ) ;
% Mi ght be used t o % i ni t i al i z e var i abl es
i nvoke( r es ul ul t , ' Save' , ' C: \ t mp\ f i l ename' ) ; A = i mpor t dat a( ' C: \ t mp\ f i l ename' , ' ' , 4) x( : , i i i ) = A. dat a( : , 1) ; % x- dat a c ol ol umn y( : , i i i ) = A. dat a( : , 2) ; % y- dat a c ol ol umn end i nvoke voke(( mws, ' Save' ve' ) ; i nvoke ke(( mws, ' Qui t ' ) ; r el r el r el r el
eas e( e( r es ul ul t ) ; eas e( e( s ol ol ver ) ; ease( mws) ; eas e( e( c s t ) ;