FRED如何调用Matlab?

简介:FRED作为COM组件可以实现与Excel、VB、Matlab等调用来完成庞大的计算任务或画图,本文的目的是通过运行一个案例来实现与Matlab的相互调用,在此我们需要借助脚本来完成,此脚本为视为通用型脚本。

 

配置:在执行调用之前,我们需要在Matlab命令行窗口输入如下命令:

enableservice('AutomationServer', true)

enableservice('AutomationServer')

FRED如何调用Matlab?的图1

结果输出为1,这种操作方式保证了当前的Matlab实体可以用于通信。

 

在winwrp界面,为增加和使用Matlab类型的目录库,我们需要如下步骤:

1. 在FRED脚本编辑界面找到参考.

2. 找到Matlab Automation Server Type Library

3. 将名字改为MLAPP

 

 

在Matlab里面有两种常用的数据发送选项PutWorkspaceData 及PutFullMatrix,PutWorkspaceData适用于存储一般的数据在工作区,并赋予其为变量,PutFullMatrix试用于复数数据。

 

FRED如何调用Matlab?的图2

图 编辑/参考

 

FRED如何调用Matlab?的图3

 

现在将脚本代码公布如下,此脚本执行如下几个步骤:

1. 创建Matlab服务器。

2. 移动探测面对于前一聚焦面的位置。

3. 在探测面追迹光线

4. 在探测面计算照度

5. 使用PutWorkspaceData发送照度数据到Matlab

6. 使用PutFullMatrix发送标量场数据到Matlab中

7. 用Matlab画出照度数据

8. 在Matlab计算照度平均值

9. 返回数据到FRED中

 

代码分享:

 

Option Explicit

 

Sub Main

 

    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

 

    'Release resources

    Set Matlab = Nothing

 

End Sub

 

最后在Matlab画图如下:

FRED如何调用Matlab?的图4

 

并在工作区保存了数据:

 

FRED如何调用Matlab?的图5
 

并返回平均值:
 

FRED如何调用Matlab?的图6

与FRED中计算的照度图对比:

   

FRED如何调用Matlab?的图7

例:

 

此例系统数据,可按照此数据建立模型
 

系统数据
 

FRED如何调用Matlab?的图8

 

光源数据:

Type: Laser Beam(Gaussian 00 mode)

Beam size: 5;

Grid size: 12;

Sample pts: 100;

相干光;

波长0.5876微米,

距离原点沿着Z轴负方向25mm。

 

对于执行代码,如果想保存图片,请在开始之前一定要执行如下代码:

enableservice('AutomationServer', true)

enableservice('AutomationServer')



FREDMATLAB

FRED如何调用Matlab?的评论0条

    暂无评论

    FRED如何调用Matlab?的相关案例教程

    简介:本文是以十字元件为背景光源,经过一个透镜元件成像在探测器上,并显示其热成像图。 成像示意图 首先我们建立十字元件命名为Target 创建方法: 面1 : 面型:plane 材料:Air 孔径:X=1.5, Y=6,Z=0.075,形状选择Box 辅助数据: 首先在第一行输入temperature :300K, emissivity:0.1; 面2 : 面型:plane 材料:Air 孔径:X
    使用FRED的图形用户界面和它的内置脚本语言,我们可以轻松地实现热辐射和成像。尽管强力的光线追迹同样是可能的,FRED使用了应用标准光学工程算法的高效运算器来实现热成像和辐射计算。使用源自辐射度量学的技术,用FRED追迹必要数量光线的可能需要的时间,我们可以高效并精确地完成热成像、冷反射、杂散光、热照明均匀性和热自发辐射的计算。 1. 热辐射和热成像是什么? 热成像定义为产生一个场景的可视化二维图
    介绍 沃拉斯顿棱镜偏振器包含两个由单轴晶体构成的直角棱镜,如方解石,它经常用于沃拉斯顿棱镜中。两块单轴晶体是定向的,使得晶轴互相垂直。如下图所示的几何结构,水平偏振光在第一个区域中以非寻常折射率(ne)传播,折射到第二个区域中,以寻常折射率(no)传播。垂直偏振光经过相反的折射,两个偏振态就这样分开了。本文介绍了一个脚本,即wollastonCreator.frs,根据输入到基本对话框中的用户技术
    PFC2D大尺度离散元快速成样技术 前言 离散元的成样方法有很多,比如颗粒膨胀法、分层压实法、落雨法等。这些成样方法都能够很好地生成均匀的试样,但是面临大尺度的离散元模型试验模拟时,巨大的颗粒数目使得个人PC的计算能力日益窘迫。本文将介绍由 Matteo Oryem Ciantia 在文章 Numerical techniques for fast generation of large disc
    我们在做CATIA 二次开发的时候,有的情况下需要暂停程序运行,让CATIA飞一会。 那么,CATIA VBA 获取延时较为可行的四个方法: 1、一般延时 Timer函数(计时单位为秒级,1代表1s) 一个应用接口需要限制运行速度,需要在循环中加个延时函数,这个延时不需要多么精确,要求有个几秒延时,网上用的比较多的就是用Timer函数编写,也是在VB联机手册中所推荐的Timer是VBA自带的函数,
    影响力
    粉丝
    内容
    获赞
    收藏
      项目客服
      培训客服
      0 0