rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
remote_function.m
1 function [varargout] = remote_function(functionname,varargin);
2 %function res = remote_function(functionname,varargin);
3 %
4 % function starting a remote matlab session, performing a function
5 % call and returning the results
6 
7 % Bernard Haasdonk 22.4.2010
8 
9 % path to windows-directory with linux commands (ssh, scp:)
10 path_bash = 'C:\Programme\Git\bin';
11 path_remote = '~/matlab/';
12 fn_res = 'tmpres.mat'; % local and remote
13 fn_arg = 'tmparg.mat'; % local and remote
14 fn_cmd = 'tmpcmd.m'; % remote command file
15 fn_arg_local = fullfile(rbmatlabtemp,fn_arg);
16 fn_ares_local = fullfile(rbmatlabtemp,fn_res);
17 fn_arg_remote = fullfile(path_remote,fn_arg);
18 fn_cmd_remote = fullfile(path_remote,fn_cmd);
19 fn_res_remote = fullfile(path_remote,fn_res);
20 
21 if nargout==0
22  error('remote function call only works for functions with return values!');
23 end;
24 
25 saveargs = varargin;
26 for i=1:length(saveargs)
27  if isobject(varargin{i})
28  warning('off', 'MATLAB:structOnObject');
29  saveargs{i} = struct(varargin{i});
30  warning('on', 'MATLAB:structOnObject');
31  end
32 end
33 save(fn_arg_local,'saveargs');
34 
35 % copy file to remote host
36 %cmd = [fullfile(bashpath,'cat'),' ',fn_arg_local];
37 cmd = [fullfile(path_bash,'ssh.exe'),...
38  ' haasdonk@am100.mathematik.uni-stuttgart.de ls'];
39 %cmd = [fullfile(bashpath,'ssh.exe'),' ',fn_arg_local,...
40 % ' haasdonk@am100.uni-stuttgart.de:', ...
41 % fn_arg_remote];
42 disp(['executing:']);
43 disp(cmd);
44 
45 keyboard;
46 
47 [s,w] = unix(cmd);
48 
49 
50 % on remote host generate command file:
51 % matlab -nodesktop -nosplash,
52 % load 'remote_fn_arg'
53 % [varargout{1:nargout}] = feval('functionname',saveargs{:});
54 % tmpfn_res = '~/tmpres.mat';
55 % save(tmpres.mat,'varargout');
56 
57 % on remote host: delete command, argument and result file
58 
59 % copy file to local host
60 
61 tmpfn_res = fullfile(rbmatlabtemp,fnres);
62 loaded = load(tmpfn_res);
63 varargout = loaded.varargout;