且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

命令行Matlab中的vi输入模式?

更新时间:2023-01-15 19:15:50

是的,这应该很容易.这只是一般的打开进程并绑定到其stdin和stdout"问题的特例,这并不困难.

Yes, that should be easy enough. It's just a special case of the general "open a process and bind to its stdin and stdout" problem, and that's not difficult.

一些Google搜索发现IO.popen()是正确的Ruby,这里的答复中有更多细节:

A bit of Google searching finds that IO.popen() is the right piece of Ruby for that, and there's a little more detail in the replies here: http://groups.google.com/group/ruby-talk-google/browse_thread/thread/0bbf0a3f1668184c. Hopefully, that's enough to get you started!

更新:看来您的包装已经快到了.您需要完成的工作是在Matlab要求输入时识别出,然后仅要求用户输入.我建议尝试使用此伪代码:

Update: Looks like you're almost there with your wrapper. What you need to get finished is recognize when Matlab is asking for input, and only ask the user for input then. I'd suggest trying this pseudocode:

while input_line = Readline.readline('>> ', true)
  io.puts input_line
  while ((output_line = io.gets) != '>> ')  // Loop until we get a prompt.
    puts io.gets
  end
end

那是不对的,因为在请求第一条输入行之前,您需要做一次内循环,但这应该可以给您带来灵感.您可能还需要调整其所要查找的提示文本.

That's not quite right, as you need to do the inner loop once before you ask for the first input line, but it should give you the idea. You might need to adjust the prompt text that it's looking for, too.

更新2:好的,所以我们还需要考虑以下事实:提示后没有EOL,因此io.gets将挂起.这是一个修改后的版本,它使用了以下事实:您可以在Matlab提示符后添加空白行,而它只会给您另一个提示而无需执行任何操作.我重新安排了循环以使事情更清楚一些,尽管这意味着您现在必须添加逻辑来确定何时完成.

Update 2: Okay, so we also need to account for the fact that there's no EOL after a prompt and so io.gets will hang. Here's a revised version that uses the fact that you can give a blank line to a Matlab prompt and it will just give you another prompt without doing anything. I've rearranged the loop to make things a little clearer, though this means you now have to add logic to figure out when you're done.

while [not done]   // figure this out somehow
  io.puts blank_line                        // This will answer the first
                                            // prompt we get.
  while ((output_line = io.gets) != '>> ')  // Loop until we get a prompt.
    puts io.gets                            // This won't hang, since the
  end                                       // prompt will get the blank
                                            // line we just sent.

  input_line = Readline.readline('>> ', true)  // Get something, feed it
  io.puts input_line                           // to the next prompt.

  output_line = io.gets   // This will eat the prompt that corresponds to
                          // the line we just fed in.
end