Dim ana As T_ANALYSIS
Dim move As T_OPERATION
Dim Matlab As MLApp.MLApp
Dim detNode As Long, detSurfNode As Long, anaSurfNode As Long
Dim raysUsed As Long, nXpx As Long, nYpx As Long
Dim irrad() As Double, imagData() As Double, reals() As Double, imags() As Double
Dim z As Double, xMin As Double, xMax As Double, yMin As Double, yMax As Double
Dim meanVal As Variant
Set Matlab = CreateObject("Matlab.Application")
ClearOutputWindow
'Find the node numbers for the entities being used.
detNode = FindFullName("Geometry.Screen")
detSurfNode = FindFullName("Geometry.Screen.Surf 1")
anaSurfNode = FindFullName("Analysis Surface(s).Analysis 1")
'Load the properties of the analysis surface being used.
LoadAnalysis anaSurfNode, ana
'Move the detector custom element to the desired z position.
z = 50
GetOperation detNode,1,move
move.Type = "Shift"
move.val3 = z
SetOperation detNode,1,move
Print "New screen position, z = " &z
'Update the model and trace rays.
EnableTextPrinting (False)
Update
DeleteRays
TraceCreateDraw
EnableTextPrinting (True)
'Calculate the irradiance for rays on the detector surface.
raysUsed = Irradiance( detSurfNode, -1, ana, irrad )
Print raysUsed & " rays were included in the irradiance calculation.
'When using real number data to send to MATLAB, it is simplest to use PutWorkspaceData.
Matlab.PutWorkspaceData("irradiance_pwd","base",irrad)
'PutFullMatrix is more useful when actually having complex data such as with
'scalar wavefield, for example. Note that the scalarfield array in MATLAB
'is a complex valued array.
raysUsed = ScalarField ( detSurfNode, -1, ana, reals, imags )
Matlab.PutFullMatrix("scalarfield","base", reals, imags )
Print raysUsed & " rays were included in the scalar field calculation."
'Calculate plot characteristics from the T_ANALYSIS structure. This information is used
'to customize the plot figure.
xMin = ana.posX+ana.AcellX*(ana.Amin-0.5)
xMax = ana.posX+ana.AcellX*(ana.Amax+0.5)
yMin = ana.posY+ana.BcellY*(ana.Bmin-0.5)
yMax = ana.posY+ana.BcellY*(ana.Bmax+0.5)
nXpx = ana.Amax-ana.Amin+1
nYpx = ana.Bmax-ana.Bmin+1
'Plot the data in Matlab with some parameters calculated from the T_ANALYSIS
'structure. Set the axes labels, title, colorbar and plot view.
Matlab.Execute( "figure; surf(linspace("&xMin &","&xMax &","&nXpx &"),linspace("& yMin &"," & yMax & "," & nYpx & "),irradiance_pwd, 'EdgeColor', 'None');" )
Matlab.Execute( "xlabel('X Position (" & GetUnits() & ")')" ) : Matlab.Execute( "ylabel('Y Position (" & GetUnits() & ")')" ) : Matlab.Execute( "zLabel( 'Irradiance' )" )
Matlab.Execute( "title('Detector Irradiance')" )
Matlab.Execute( "colorbar" )
Matlab.Execute( "view(2)" )
Print ""
Print "Matlab figure plotted..."
'Have Matlab calculate and return the mean value.
Matlab.Execute( "irrad = mean(mean(irradiance_pwd));" )
Matlab.GetWorkspaceData( "irrad", "base", meanVal )
Print "The mean irradiance value calculated by Matlab is: " & meanVal