How to/Can I send commands to Matlab remotely

35 visualizzazioni (ultimi 30 giorni)
Jay Kemper
Jay Kemper il 18 Lug 2012
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."

Risposte (2)

Albert Yam
Albert Yam il 18 Lug 2012
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 Commento
Albert Yam
Albert Yam il 18 Lug 2012
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.

Accedi per commentare.


Jason Ross
Jason Ross il 18 Lug 2012
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 Commenti
Jay Kemper
Jay Kemper il 18 Gen 2013
Modificato: Jay Kemper il 18 Gen 2013
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 il 23 Gen 2013
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.

Accedi per commentare.

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by