【问题标题】:Start application from Matlab从 Matlab 启动应用程序
【发布时间】:2011-12-01 11:43:57
【问题描述】:

我正在寻找一种从 Matlab 中启动应用程序的方法。问题是,我的 Matlab 脚本将一些结果保存到一个文件中,然后应该在关联的应用程序(本例中为 Blender)中打开该文件。

我熟悉诸如

之类的命令
system('program_name')

!program_name

还有其他一些方法,但问题是,应用程序是使用 Matlab PATH 启动的,因此它会在 Matlab 目录中查找所需的各种库。例如:

>> !blender
blender: /usr/local/MATLAB/R2011a/sys/os/glnx86/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by blender)

有什么方法可以启动使用全局(系统)PATH 的应用程序吗?

刚才我想我找到了一个调整,即从 Matlab 中启动一个终端,带有一些参数(Blender 文件名.blend)。

system('terminal -x blender /home/pieter/Red.blend')

这确实工作了几次,但现在我在执行此命令 20 次左右后出现错误...

>> system('terminal -x blender /home/pieter/Red.blend')
(terminal:10982): GLib-CRITICAL **: PCRE library is compiled without UTF8 support
(terminal:10982): Terminal-CRITICAL **: Failed to parse regular expression pattern 0: PCRE library is compiled without UTF8 support

顺便说一句,我正在使用 Arch Linx。


编辑

好吧,我只是想到了一个相当肮脏的解决方案。 matlab使用环境变量

LD_LIBRARY_PATH

对于必要库的路径:

getenv('LD_LIBRARY_PATH')
/usr/local/MATLAB/R2011a/sys/os/glnx86:/usr/local/MATLAB/R2011a/bin/glnx86:/usr/local/MATLAB/R2011a/extern/lib/glnx86:/usr/local/MATLAB/R2011a/runtime/glnx86:/usr/local/MATLAB/R2011a/sys/java/jre/glnx86/jre/lib/i386/native_threads:/usr/local/MATLAB/R2011a/sys/java/jre/glnx86/jre/lib/i386/client:/usr/local/MATLAB/R2011a/sys/java/jre/glnx86/jre/lib/i386

所以我可以将此信息保存到一个变量中(例如 MatlabPath):

MatlabPath = getenv('LD_LIBRARY_PATH')

然后在我调用搅拌机之前这样做:

setenv('LD_LIBRARY_PATH',getenv('PATH'))

这使得 Matlab 使用我的系统库。然后在程序启动后,将旧值重新分配给 LD_LIBRARY_PATH:

setenv('LD_LIBRARY_PATH',MatlabPath)

所以...这是一个解决方案,但如果有人知道解决问题的更简洁的方法,请告诉我。

【问题讨论】:

  • 您在“编辑”中写的内容是我所知道的最佳解决方案。您还可以编辑 mexopts.sh 文件以在每次启动时根据需要设置环境变量。
  • 感谢您的建议 :)。
  • 听起来不错,我会写点什么。我还有另一个相关问题,如何使用一个或多个参数(如变量文件名)启动应用程序,以使应用程序在Matlab 外部运行?我的意思是,!program_name & 对应用程序执行此操作(添加 & 符会执行此操作),但是如何添加参数? system('program_name')...是否有类似的可能?
  • 如果您使用 shell 而不是终端应用程序,生成另一个 shell 是否有效? IE。 system(['bash -c ' commandStr])?

标签: linux matlab


【解决方案1】:

正如我在上面的编辑中指出的那样,这可能是一个解决方案:

% Save library paths
MatlabPath = getenv('LD_LIBRARY_PATH');
% Make Matlab use system libraries
setenv('LD_LIBRARY_PATH',getenv('PATH'))
disp('Starting Blender...')
system( ['blender ', Directory, FileName, '.blend'] )
% Reassign old library paths
setenv('LD_LIBRARY_PATH',MatlabPath)

但是,使用其他方式启动应用程序,您可以在启动后立即返回 Matlab:

% Start Blender and immediately return to Matlab
!blender Geometry.blend &

& 符号 (&) 是启动应用程序后立即返回 Matlab 的关键,但是以这种方式启动 Blender 我无法像 system(. ..).

所以,有人知道如何做

  • !program_name 与变量文件名一起使用

  • system(program_name) 与一个选项一起使用,这样 Matlab 只会启动应用程序(并且不会等到应用程序关闭后才返回)

【讨论】:

    【解决方案2】:

    只需在 MATLAB 中运行命令:

    setenv('LD_LIBRARY_PATH',[getenv('PATH') getenv('LD_LIBRARY_PATH')])

    它在系统库中附加了matlab库。

    【讨论】:

      【解决方案3】:

      您实际上可以在系统调用中清除 LD_LIBRARY_PATH 变量,如下所示:

      system('LD_LIBRARY_PATH=; blender');
      

      (请注意,这很可能取决于 MATLAB 内部启动的 shell 的命令语法。以上内容应该适用于 Bash)。

      【讨论】:

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