How to control MATLAB on remote PC from MATLAB command window on another local PC
5 views (last 30 days)
Show older comments
I'm trying to control Matlab on remote PC in local area network from local Matlab.
I don't want to use VNC client because I need to control Matlab on remote PC from command window or matlab script in local Matlab.
Can anyone tell me the best way to solve this probrem?
0 Comments
Answers (1)
Mark Whirdy
on 13 Dec 2012
Edited: Mark Whirdy
on 13 Dec 2012
I did something similar, running an matlab/tomlab/cplex optimization job on a server called from my local machine. I wrote a C# class to log into the remote machine and run a .bat file there (process below).
[Matlab (local)] --> [Matlab's .Net Interface runs DLL] --> [DLL written in C# logs into remote machine & calls .bat file] --> [.bat file opens matlab on remote machine] --> [Matlab (remote)] --> [m-files (remote)]
The C# class is below, I'll have to go back & recompile it to ensure that it works 100% though.
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
//using System.Security;
//using System.Security.Permissions;
//using System.Reflection;
//using System.Runtime.CompilerServices;
using System.Reflection;
using System.Security.Permissions;
using System.Security;
using System.IO;
//[assembly: AllowPartiallyTrustedCallers]
/*
* Overview:
* RemoteExecution.cs provides for the RemoteExecution class; facilitating the execution of programs
* on a remote machine through username/password authentication on the IP-Address of the remote machine
* and the path of the target executable (or batch file). RemoteExecution strongly inherits from System.Management
*
* Usage:
* Used primarily to allow matlab to connect to Dub1wsp02804 programmatically, through NET
* Interface to RemoteExecution.dll.
*
* Example:
* ipAddress: "10.250.33.174"
* userName: "mwhirdy"
* passWord: "*********"
* targetProcessPath: "\\Dub1wsp02804\c$\QuantCredit\Matlab\iCreditMaster\testBatchFileRoutine.bat"
*
* Written By:
* Mark Whirdy (09-Nov-2010)
* */
namespace iCreditNETLibrary
{
public class RemoteExecution
{
/*
String userName = "";
String passWord = "";
String ipAddress = "";
String targetProcessPath = "";
*/
FileIOPermission perm = new FileIOPermission(PermissionState.Unrestricted); // <<<< TEMPORARY TROUBLESHOOTING LINE
public string userName, passWord, ipAddress, targetProcessPath;
// public string ConnectAndExecute(string userName, string passWord, string ipAddress, string targetProcessPath)
public string ConnectAndExecute()
{
perm.Assert(); // <<<< TEMPORARY TROUBLESHOOTING LINE
// Prime Connection Object with UserName and Password Explicitly
ConnectionOptions connection = new ConnectionOptions();
connection.Username = userName;
connection.Password = passWord;
// Attempt Connection To ipAddress through ConnectionObject
ipAddress = String.Format("\\\\{0}\\root\\cimv2", ipAddress);
ManagementScope MgmtScope = new ManagementScope(ipAddress, connection);
try
{
MgmtScope.Connect();
}
catch (Exception e)
{
return e.Message;
}
// Attempt CommandLine Creation of Win32 Process
ManagementClass MgmtClass = new ManagementClass(MgmtScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
ManagementBaseObject inParams = MgmtClass.GetMethodParameters("Create");
inParams["CommandLine"] = targetProcessPath;
try
{
ManagementBaseObject outParams = MgmtClass.InvokeMethod("Create", inParams, null);
}
catch (Exception e)
{
return e.Message;
}
// Return Executed KeyWord to indicate successful execution
return "Executed";
}
}
}
Your local m-file which calls this C# method (once you compile the above as a DLL) will be something like
assemblyInfo = NET.addAssembly(fullfile(dllPath,'iCreditNETLibrary.dll')); %#ok
objectRemoteExecution = iCreditNETLibrary.RemoteExecution; % Instantiate object of class <RemoteExecution>
userName = getenv('username');
objectRemoteExecution.ipAddress = '10.250.33.174'; % IP Address for Dub1wsp02804
objectRemoteExecution.userName = userName;
objectRemoteExecution.passWord = passWord;
remoteServerBatchPath = '\\Dub1wsp02804\c$\QuantCredit\Matlab\iCredit\iCredit Remote Optimization Layer';
% batchFileName = 'portfoptBatchRoutine.bat';
batchFileName = 'Callee_RemoteExecutionTestEmailer.bat'; objectRemoteExecution.targetProcessPath = fullfile(remoteServerBatchPath,batchFileName);
outputFlag = objectRemoteExecution.ConnectAndExecute; % call <ConnectAndExecute> method
The batch file then will be something like:
start matlab -nosplash -nodesktop -minimize -r "cd('C:\mypath\myfolder'); pause(3); myMFile; exit"
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!