matlab - 是否可以从命令行在 Matlab 的运行实例中打开文件?

标签 matlab

如果我已经有一个正在运行的 Matlab 实例,是否可以告诉我从 Matlab 应用程序外部在 Matlab 编辑器中打开一个文件?我想知道是否有可能做这样的事情。

启动一个 Matlab 实例

$ ./matlab 

使用已经运行的 Matlab 实例打开文件进行编辑:

$ matlab_open_file.sh theFile.m

GUI 变体是从文件夹中拖动文件,然后将其放到 Matlab 图标上(这实际上在 OS X 下有效)

注意 我知道您可以启动 Matlab 并让它立即执行命令(您可以使用它在启动时启动编辑器)。这不是我想要的。

最佳答案

我编写了一个适用于 Linux 的变通方案(在 Mint 17.1 和 R2014a 和 R2014b 上运行),然后我将其与 .fig.m 文件扩展名相关联。请注意,这需要安装 xdotool,并且为 Windows 快捷方式设置了击键(默认情况下,MATLAB 附带 Linux 上的 Emacs 快捷方式,但根据我的经验,几乎每个人都会更改它们)。这有一个限制,即当前命令行上的任何文本都会被删除,并且有一个小的时间窗口,MATLAB 不能失去焦点。但在没有更强大的解决方案的情况下,它对我来说已经足够好了。

#!/bin/bash

# Hacky way to open a MATLAB figure in an existing instance if there is
# one, and start a new instance if not.

# What are we trying to open?
FILENAME="$@";

# Try to identify the main MATLAB window.
MLWINDOW=$( comm -12\
              <(xdotool search --name MATLAB\ R | sort)\
              <(xdotool search --class "com-mathworks-util-PostVMInit" | sort) )
if [ -z "$MLWINDOW" ]; then
    # MATLAB isn't open; we have to open it to proceed.
    matlab -desktop -r "open('$FILENAME')"
else
    # We use the first existing instance since MATLAB is open
    set -- $MLWINDOW
    # Jump to the command line and erase it
    xdotool windowactivate --sync $1 key --delay 0 "control+0" Escape
    # Put the filename on the command line
    xdotool type --delay 0 "$FILENAME"
    # Select the filename and press ctrl-D to open, then clean up command line
    xdotool key --delay 0 "shift+Home" "control+d" Escape
fi

关于matlab - 是否可以从命令行在 Matlab 的运行实例中打开文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12862871/

相关文章:

matlab - 控制分割精度

image - 如何遍历矩阵中的一列?

matlab - 基本的matlab帮助

matlab - imnoise 高斯噪声澄清

matlab - MATLAB 中的 Canny 边缘检测器

面向程序员的 MATLAB 教程

java - Matlab 结构和 Java LinkedList

arrays - 将元胞矩阵转换为单行向量?

matlab - 在 Matlab 中重复向量的最有效方法

matlab - 如何将图片插入手机壳