【问题标题】:How to run a matlab function remotely fron Fortran code如何从 Fortran 代码远程运行 matlab 函数
【发布时间】:2015-08-05 19:29:55
【问题描述】:

我有一个很长(大约 1000 行)和复杂的 Matlab 函数(比如 mat_test.m),它工作得非常好。另一方面,我有一组复杂的 Fortran 代码。我正在尝试在其中一个 Fortran 代码中插入我的 Matlab 函数。我在 Fortran 方面没有太多能力,所以我不想在 Fortran 中重写整个 Matlab 函数。有没有办法在 Fortran 中定义变量并远程调用这个 Matlab 函数并使用它生成的输出?

【问题讨论】:

    标签: matlab fortran


    【解决方案1】:

    确实可以从 fortran 代码中调用 matlab 函数。 Matlab 在 fengdemo.F 文件中的 matlabroot/extern/examples/eng_mat 文件夹中包含一个详细示例,说明如何正确实现此功能。

    Here is the file showing how it's done.

    Here is the example output of this file, from the mathworks website.

    【讨论】:

    • 编辑得更具体一些,包括示例文件。
    • 那么我应该用我的 Matlab 函数 (mat_test.m) 和其他变量相应地替换 mxCopyReal8ToPtr 还是有不同的方法?
    【解决方案2】:

    您可以使用 Fortran 执行或更好地启动程序

    call SYSTEM("script.m")
    

    现在,您编辑您的 Matlab 脚本以将数据保存在 .txt 文件中; 然后您可以在 Fortran 中编写代码来读取该文件;

    我的想法是这样的:

    ! ...do something in Fortran
    call SYSTEM("modified_script.m")
    ! read data.txt
    ! continue your works with data from Matlab...
    

    PS:你不需要包含任何库

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-26
      相关资源
      最近更新 更多