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

时间:2012-10-12 15:55:58

标签: matlab

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

启动Matlab实例

$ ./matlab 

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

$ matlab_open_file.sh theFile.m

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

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

4 个答案:

答案 0 :(得分:2)

我为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

答案 1 :(得分:1)

您可以在命令行中输入路径+文件名,如果matlab会话打开,它将在当前的matlab会话中打开此文件。

请注意,这仅在您确保matlab是打开此类文件的默认程序时才有效。 (用.m文件测试)

答案 2 :(得分:0)

我修改了Aoeuid's approach,因为

  • 它对我不起作用,因为我已经重新分配Ctrl+0跳转到命令行(我不知道我可以将其设置为另一个值)→我将其替换为“打开文件“对话框(Ctrl+O)。
  • 我可能想要打开不在当前matlab路径上的脚本→我使用$PWD/$filename而不是$filename。您可以使用open($PWD/$FILENAME)KP_Enter代替$FILENAMEshift+Home / control+d来修改其版本。

结果如下:

#!/bin/bash

filename="$1"

# 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('$PWD/$filename')"
else
    ## Activate window:
    xdotool windowactivate --sync $MLWINDOW && \
    ## Press Ctrl+O to open the "open" dialog:
    xdotool key --delay 0 "control+o" && \
    ## Wait for the file dialog to open:
    sleep 0.5 && \
    ## Type the file name including the current directory
    xdotool type --delay 0 "$PWD/$filename" && \
    ## Press enter:
    xdotool key "KP_Enter"
fi

但是,使用按键进行自动过程可能会导致不必要的结果。

答案 3 :(得分:-2)

确保您已将文件夹添加到路径。

然后你去你需要的文件夹。

然后输入Matlab终端

your_program_name

然后你的程序就会运行。