FRED如何调用Matlab

[复制链接]
cherryjhy 发表于 2023-5-10 09:20:14 | 显示全部楼层 |阅读模式
简介:FRED作为COM组件可以实现与Excel、VB、Matlab等调用来完成庞大的计算任务或画图,本文的目的是通过运行一个案例来实现与Matlab的相互调用,在此我们需要借助脚本来完成,此脚本为视为通用型脚本。

配置:在执行调用之前,我们需要在Matlab命令行窗口输入如下命令:
enableservice('AutomationServer', true)
enableservice('AutomationServer')
1-16011R12525G7.png.jpg
结果输出为1,这种操作方式保证了当前的Matlab实体可以用于通信。

在winwrp界面,为增加和使用Matlab类型的目录库,我们需要如下步骤:
1. 在FRED脚本编辑界面找到参考.
2. 找到Matlab Automation Server Type Library
3. 将名字改为MLAPP


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

1-16011R12559B8.png.jpg

图 编辑/参考


1-16011R12A55Z.png.jpg


现在将脚本代码公布如下,此脚本执行如下几个步骤:
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



回复

使用道具 举报

全部回复0 显示全部楼层
暂无回复,精彩从你开始!

快速回帖

您需要登录后才可以回帖 登录 | 立即注册 手机动态码快速登录

本版积分规则

关于楼主

高级会员
  • 主题

    820
  • 回答

    812
  • 积分

    821
联系客服 关注微信 访问手机版 返回顶部 返回列表