- 易迪拓培训,专注于微波、射频、天线设计工程师的培养
CST与Matlab连接设置
4. 宏VBA代码:
Option Explicit
Sub Main
'User defined settings
Dim matlab_visible As Boolean
Dim user_path, save_file_name As String
matlab_visible = True 'True to keep
Matlab command window open, False to not display it
user_path = "C:Program Files
CST Matlab data" 'path that Matlab opens to and any data is saved to
save_file_name = "cst_data" 'name of .mat file matlab saves all data to
'variable defn
'------------------------------------------------------------------------------------
'Matlab variables
Dim matlab As Object
Dim Result As String
Dim sendA(2) As Double
'set up variables
Dim NumPorts, NumModes, NumFre
q, NumEng, NumTime As Integer
Dim factorA, factorB As Double
Dim j, i As Integer
'Result variables
Dim eng_obj, sig_in_obj, si
g_out_obj As Object
Dim file_nameA As String
Dim freqs() As Double
Dim times() As Double
Dim eng_time() As Double
Dim eng_A() As Double
Dim T_stop As Double
Dim sig_in() As Double
Dim sig_out() As Double
Dim MImag() As Double
Dim NumSamp As Integer
Dim Fmin, Fmax As Double
Dim S11dBD() As Double
Dim freqD() As Double
'MWS problem information
NumPorts = Solver.GetNumberOfPorts
NumModes = Solver.GetNumberOfPorts
factorA = Units.GetFrequencyUnitToSI
factorB = Units.GetTimeUnitToSI
NumSamp = Solver.GetNFsteps
Fmin = Solver.GetFmin
Fmax = Solver.GetFmax
'--------------------------------------------------------------------------------------------
'Retrieve MWS data
'------------------------------------------------------------------------------------
Set S11A = Result1D("a1(1)1(1)")
NumFreq = Solver.GetNFsteps
ReDim S11B(NumFreq-1)
ReDim freq(NumFreq-1)
ReDim S11dB(NumFreq-1)
ReDim MImag(NumFreq-1)
For j = 0 To NumFreq-1
freq(j) = S11A.GetX(j)/factorA
S11B(j) = S11A.GetY(j)
Next j
'Matlab COM/ActiveX interaction
'------------------------------------------------------------------------------------
'1) create COM object and initiate Matlab as an activeX server
Set matlab = CreateObject("Matlab.Application")
If matlab_visible Then
Result = matlab.Execute("h=actxser
ver('Matlab.Application');set(h,'visible',1);")
End If
'2) send all MWS data to Matlab using
Automation methods (see app note for listing)
Call matlab.PutCharArray("user_path","base",user_path)
Call matlab.PutCharArray("save_file_name","base",save_file_name)
Call matlab.PutFullMat
rix("S11","base",S11B,MImag)
Call matlab.PutFullMatri
x("freq","base",freq,MImag)
'3) use execute command to control Matlab engine
Result = matlab.Execute("cd(user_path)")
Result = matlab.Execu
te("S11dB=20.*log10(abs(S11));")
Result = matlab.Execu
te("plot(freq,S11dB);grid on;")
Result = matlab.Execute("title('S-pa
rameters from CST Microwave Studio');")
Result = matlab.Execute("xlabel('Freq, GHz.');")
Result = matlab.Execute("ylabel('S11, dB');")
Result = matlab.Execute("eval(['save ',save_file_name,';']);")
'4) retreive all data from Matlab to display in MWS
Call matlab.GetFullMat
rix("S11dB","base",S11dB,MImag)
'display or store Matlab data in MWS form
'------------------------------------------------------------------------------------
'1) write any data you wish to display to a file
fileA = "matlab_output.txt"
Open fileA For Output As #1
For j = 0 To NumFreq-1
Print #1,CStr(freq(j)) & " " & CStr(S11dB(j))
Next j
Close #1
'2) insert Matlab data into result tree
With ResultTree
.Reset
.Name "1D ResultsMatlabS11 dB"
.Type "XYSignal"
.Subtype "Linear"
.Title "S11 in dB computed in Matlab"
.Xlabel "Frequency, Hz"
.Ylabel "dB"
.File fileA
.Add
End With
End Sub