How to/Can I send commands to Matlab remotely

23 次查看(过去 30 天)
I have Matlab running on a computer, and I'm wondering if there is a way to send a command to Matlab from a remote computer. I understand this would require Matlab to open a port and be listening. Aside from writing a thread that runs in the background while Matlab is running, is there capability already in Matlab?
Example:
Matlab started on computer "sim"
Program in "interface" C++ code, independent program on remote computer:
#include "MatlabInterface.h"
int main(int argc,char*argv[])
{
MatlabInterface matlab("sim",PORT_NUMBER);
std::string cmd("a=4");
matlab.sendCommand(cmd);
return 0;
}
where "MatlabInterface" is a class that sets up ethernet socket calls and the "sendCommand" send a string of a certain length over that connection.
Edit:
Long story short, I'd like to run a command in the command window, but the input is an external source, not the Matlab window. Send a message to Matlab that says "Hey Matlab, run this string in your command window."

回答(2 个)

Albert Yam
Albert Yam 2012-7-18
What is it that you want to do remotely? If you want total control of Matlab, then just VNC.
If you want to run a script, my first thought was to have a urlread, read something like Etherpad or Evernote, to flip a trigger that is set on a timer function. Not sure if that would work like I imagine.
  1 个评论
Albert Yam
Albert Yam 2012-7-18
urlread reads the page as a string, have some keyword as the trigger (so you can regexp/strfind to find it) and eval the portion of the string after that. (Maybe an end keyword as well).
But I guess that is for shorter/simpler commands. How about uploading an M-file or txt-file to Dropbox/Google drive that you have access to, and urlwrite/run that directly. Start with an empty file, upload one with commands remotely. Not sure how files are saved there, and/or if the url changes.

请先登录,再进行评论。


Jason Ross
Jason Ross 2012-7-18
A few ways I can think if doing this:
  • Parallel Computing Toolbox can have a MATLAB worker running on a remote server. When you submit a command, it runs on the remote machine's MATLAB worker.
  • Instrument Control Toolbox has a way of providing communication over TCP/IP
  • There is a TCP/UDP/IP Toolbox in the File Exchange
  • You could use the iPhone/iPad application and connector to connect back to a MATLAB running on your desktop.
  • Traditional remote control applications: VNC, Remote Desktop
  • If the remote machine is Linux, you can use ssh/rsh to run MATLAB in a terminal shell
There are probably a few others I'm missing or that other people have done/implemented over time. Albert's suggestion of a polling function would also work, with the remote session looking for work on some set interval. Or finding work, generating an m script, then running MATLAB, passing the name of the script to it and returning results.
  2 个评论
Jay Kemper
Jay Kemper 2013-1-18
编辑:Jay Kemper 2013-1-18
This is very close. I have the Parallel Computing toolkit, and Java can be used for the Socket command. I have successfully written a function that will listen for a string and then execute that string via "evalin('base','command')".
For a simple example, I use the function
function [] = thefunction()
evalin('base','g=56');
end
That creates a variable g and sets it to 56, but when I run
batch('thefunction',0,{});
it doesn't do anything of the sort in the base workspace. Any ideas?
Jason Ross
Jason Ross 2013-1-23
The worker that's running remotely is an entirely different process than a MATLAB that would be started on the remote machine, so I don't think they are going to communicate as you would like.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Java Package Integration 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by