Reputation: 123
Has anybody an idea how I'm able to run a matlab .m file out of my tcl script on my mac. I want to do something linke this:
definition of some variables in my .tcl script:
# run_matlab.tcl:
set a 1;
set b 2;
set c 3;
open matlab test.m and perform some calulations with the predefined variables (predefined in tcl), e.g.:
% test.m
D = [a b c];
E = [c b a]';
F = D*E
back in tcl set new variables based on F (calculated in matlab) and perform some more calulations with F, e.g.:
# run_matlab.tcl:
set m $F;
set n [expr 3*$m];
puts $n
I'm an absolut newbie and have no clue how to handle this problem. Can anybody help me??
I've done something, but I'm not 100% satisfied with this. My solution looks like the following:
# test.tcl
# parameter definition
set a 7;
set b 5;
# calculation of 'e' in matlab
exec /Applications/MATLAB_R2012a.app/bin/matlab -nosplash -nodesktop -r test_matlab($a,$b);
# input calculated variables
# c = a+b = 2
# d = a-b = 12
source output.tcl
# do further calculations
set e [expr $c+$d];
puts $e
And the Matlab .m file does look like this:
function test_matlab(a,b)
% calculate a and b
c = a+b;
d = a-b;
% output
fprintf(fopen(['output.tcl'],'a+'),'set c %f;\n',c);
fprintf(fopen(['output.tcl'],'a+'),'set d %f;\n',d);
% quit matlab
quit
end
So someone can see, that I'e to load my calculated data with 'source output.tcl'.
BUT: Is there a way to get my variables directly into tcl variables? And whats about lists? If I've calculated a vector in matlab, how can I directly save this vector in to a list?
Upvotes: 1
Views: 2069
Reputation: 246764
You could get matlab to simply print the two values (either on separate lines or space-separated on one line). Then, in Tcl:
set output [exec matlab ...]
lassign [split $output] c d
# do stuff with $c and $d
Upvotes: 0
Reputation: 4732
I'm not experienced with TCL. But I'm sure you can get it to call a command with arguments. Basically you got two options:
Call Matlab from command line (see, especially matlab -r "statement"
) and put your declarations in the call.
You could setup your Matlab script as server and have it listen on some port where you send your commands to and receive answers. On windows you can also connect to Matlab using COM and send commands - but I couldn't find if similar functionality is available on Mac/Linux.
In addition you might consider compiling your script with mbuild. Not only will it then be possible to distribute it - but I think if you compile to java it might be easier to integrate in TCL.
Upvotes: 0