9

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

启动 Matlab 实例

$ ./matlab 

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

$ matlab_open_file.sh theFile.m

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

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

4

4 回答 4

2

我为 Linux 编写了一个解决方法(在具有 R2014a 和 R2014b 的 Mint 17.1 上运行),然后我将其与.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
于 2015-04-23T17:52:56.203 回答
1

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

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

于 2012-10-24T10:09:51.927 回答
0

我修改了 Aoeuid 的方法,因为

  • 它对我不起作用,因为我重新分配了Ctrl+0哪个跳转到命令行(而且我看不到在哪里可以将其设置为另一个值)→我将其替换为“打开文件”对话框(Ctrl+O)。
  • 我可能想打开不在当前 matlab 路径上的脚本 → 我使用$PWD/$filename而不是$filename. 您可以使用open($PWD/$FILENAME)andKP_Enter而不是$FILENAMEand shift+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

但是,将按键用于自动化过程可能会导致不需要的结果。

于 2015-06-25T08:48:19.590 回答
-3

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

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

只需在 Matlab 终端中输入

your_program_name

然后你的程序就会运行。

于 2014-10-07T06:46:03.810 回答