Thank you sir for your prompt reply. Currently i am stuck at point where I need to call an available Matlab program from an R 2.4.0 interface. How can I do this? I have downloaded the R.matlab file and also the manual in pdf. But still i am not able to get through the problem. I will be grateful to you if you can elaborate me on this. Awaiting your reply, regards, Bhanu Kalyan K Bhanu Kalyan K BTech CSE Final Year reach4kalyan@gmail.com Tel :+91-9885238228 __________________________________________________ [[alternative HTML version deleted]]
Hi, what operating system are you on and what version of Matlab do you have? In general you should be able to get started with R.matlab by first installing all required packages from CRAN: install.packages(c("R.oo", "R.utils", "R.matlab")) Then load the package: library(R.matlab)>From there just follow the example in help(Matlab):# Create a Matlab client matlab <- Matlab(host="localhost", port=9998) # Connect to the Matlab server if (!open(matlab)) throw("Matlab server is not running: waited 30 seconds.") # Run Matlab expressions on the Matlab server res <- evaluate(matlab, "A=1+2;", "B=ones(2,20);") # Get Matlab variables data <- getVariable(matlab, c("A", "B")) cat("Received variables:\n") str(data) ... # When done, close the Matlab client, which will also shutdown # the Matlab server and the connection to it. close(matlab) Hope this helps Henrik On 12/15/06, Bhanu Kalyan.K <kalyansikha at yahoo.com> wrote:> Thank you sir for your prompt reply. > Currently i am stuck at point where I need to call an available Matlab > program from an R 2.4.0 interface. How can I do this? I have downloaded the > R.matlab file and also the manual in pdf. But still i am not able to get > through the problem. I will be grateful to you if you can elaborate me on > this. > > Awaiting your reply, > > regards, > Bhanu Kalyan K > > > Bhanu Kalyan K > BTech CSE Final Year > reach4kalyan at gmail.com > Tel :+91-9885238228 > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com
Sir, I am still new to the R-matlab interfacing. I will explain you the problem statement more clearly. The following is a matlab code. (swissroll.m) ======================================================== % SWISS ROLL DATASET N=2000; K=12; d=2; clf; colordef none; colormap jet; set(gcf,'Position',[200,400,620,200]); % PLOT TRUE MANIFOLD tt0 = (3*pi/2)*(1+2*[0:0.02:1]); hh = [0:0.125:1]*30; xx = (tt0.*cos(tt0))'*ones(size(hh)); yy = ones(size(tt0))'*hh; zz = (tt0.*sin(tt0))'*ones(size(hh)); cc = tt0'*ones(size(hh)); subplot(1,3,1); cla; surf(xx,yy,zz,cc); view([12 20]); grid off; axis off; hold on; lnx=-5*[3,3,3;3,-4,3]; lny=[0,0,0;32,0,0]; lnz=-5*[3,3,3;3,3,-3]; lnh=line(lnx,lny,lnz); set(lnh,'Color',[1,1,1],'LineWidth',2,'LineStyle','-','Clipping','off'); axis([-15,20,0,32,-15,15]); % GENERATE SAMPLED DATA tt = (3*pi/2)*(1+2*rand(1,N)); height = 21*rand(1,N); X = [tt.*cos(tt); height; tt.*sin(tt)]; % SCATTERPLOT OF SAMPLED DATA subplot(1,3,2); cla; scatter3(X(1,:),X(2,:),X(3,:),12,tt,'+'); view([12 20]); grid off; axis off; hold on; lnh=line(lnx,lny,lnz); set(lnh,'Color',[1,1,1],'LineWidth',2,'LineStyle','-','Clipping','off'); axis([-15,20,0,32,-15,15]); drawnow; % RUN LLE ALGORITHM Y=lle(X,K,d); % SCATTERPLOT OF EMBEDDING subplot(1,3,3); cla; scatter(Y(1,:),Y(2,:),12,tt,'+'); grid off; set(gca,'XTick',[]); set(gca,'YTick',[]); ========================================================== I must write a program in R, which will call this swissroll.m file. The output (of swissroll.m) must be shown in R interface and not in matlab . Is it possible? How can i do it? Kindly bear with me as i am not very comfortable with R. Though i have read your help(Matlab) and the examples, i did not find any clear documentation regarding calling an external file in R. Please help me on this. regards, Bhanu Kalyan K Bhanu Kalyan K BTech CSE Final Year reach4kalyan@gmail.com Tel :+91-9885238228 __________________________________________________ [[alternative HTML version deleted]]
Hi, It might be that R can't find Matlab; then you have to specify option 'matlab', see help(Matlab). Try also a different port. Try to add a line setVerbose(matlab, -2) to get more detailed output what is going on; matlab <- Matlab(host="localhost", port=9998) setVerbose(matlab, -2) if (!open(matlab)) throw("Matlab server is not running: waited 30 seconds.") If you can't get it to work, send the output of the above. /Henrik On 12/27/06, Bhanu Kalyan.K <kalyansikha at yahoo.com> wrote:> Respected Sir, > > I thank you for your concern. I have worked with the code that you have > provided. But it has generated errors like: > > > if (!open(matlab)) > + throw("Matlab server is not running: waited 30 seconds.") > //This command is not responding even after 30 seconds. > > > res <- evaluate(matlab, "swissroll") > Error in writeBin(con = con, as.integer(b), size = 1) : > invalid connection > > > vars <- getVariable(matlab, c("Y", "X", "K", "d")) > Error in writeBin(con = con, as.integer(b), size = 1) : > invalid connection > Kindly help me with this. > > Regards > Bhanu Kalyan K > > > Bhanu Kalyan K > BTech CSE Final Year > reach4kalyan at gmail.com > Tel :+91-9885238228 > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com
Hi.>From what you tell me you manage to start Matlab in the background by calling:Matlab$startServer() but that R fails to connect to Matlab by: matlab <- Matlab(host="localhost", port=9998) if (!open(matlab)) throw("Matlab server is not running: waited 30 seconds.") Sorry for not being explicit enough; if no port is given, Matlab$startServer() will setup up Matlab to listen to port 9999 (as explained in the help), but then you try to communicate with it via port 9998. I realize that the example might be a bit confusing since it is using port 9998 (for the purpose of illustrating the fact that you can choose another port). Either try the above with Matlab$startServer(port=9998), *or*, maybe simpler: Matlab$startServer() matlab <- Matlab(host="localhost") if (!open(matlab)) throw("Matlab server is not running: waited 30 seconds.") Does this work for you? Henrik On 12/27/06, Bhanu Kalyan.K <kalyansikha at yahoo.com> wrote:> > > library("R.matlab") > Loading required package: R.oo > R.oo v1.2.3 (2006-09-07) successfully loaded. See ?R.oo for help. > R.matlab v1.1.2 (2006-05-08) successfully loaded. See ?R.matlab for help. > > > help(Matlab) > > > Matlab$startServer()Loading required package: R.utils > R.utils v0.8.0 (2006-08-21) successfully loaded. See ?R.utils for help. > [1] 0 > // Here a Matlab window opened but that window couldnot be maximized. > > > matlab <- Matlab(host="localhost", port=9998) > > > matlab > [1] "Matlab: The Matlab host is 'localhost' and communication goes via port > 9998. Objects are passed via the local file system (remote=FALSE). The > connection to the Matlab server is closed (not opened)." > > > setVerbose(matlab, -2) > > > open(matlab) > Opens a blocked connection to host 'localhost' (port 9998)... > Try #0. > Try #1. > Try #2. > Try #3. > Try #4. > Try #5. > Try #6. > Try #7. > Try #8. > Try #9. > Try #10. > Try #11. > Try #12. > > There were 12 warnings (use warnings() to see them) > Opens a blocked connection to host 'localhost' (port 9998)...done > > > if (!open(matlab)) > + throw("Matlab server is not running: waited 30 seconds.") > Opens a blocked connection to host 'localhost' (port 9998)... > Try #0. > Try #1. > // I 'stopped' the computation here > Warning message: > localhost:9998 cannot be opened Opens a blocked connection to host > 'localhost' (port 9998)...done > > This is the output obtained after running the commands. Kindly go through > the above commands and help me identify the problem. > > Regards, > Bhanu Kalyan K > > > Bhanu Kalyan K > BTech CSE Final Year > reach4kalyan at gmail.com > Tel :+91-9885238228 > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com
On 12/28/06, Bhanu Kalyan.K <kalyansikha at yahoo.com> wrote:> Respected Sir, > > It worked. > > > open(matlab) > [1] TRUEGood.> > But however, the 'evaluate' function is not responding. > When i give the command as: > > > res <- evaluate(matlab, "A=1+2;", "B=ones(2,20);") > > The R interface is not returning any value though i wait for 3-4 minutes.This example should respond more or less instantaneously.> Same is the case with > > > close(matlab) > > This is not closing the matlab window, neither it is throwing any warning. > It is just asking me to wait but finally outputs no result. I am forced to > "stop the current computation".Ok, let's turn on all output you can in order to troubleshoot this. Make sure to do: setVerbose(matlab, -2) before those non-responding calls. You may also start the Matlab server in a non-minimized window by calling: Matlab$startServer(minimize=FALSE) This will allow you to see what the Matlab server is doing. What do you get? /H PS. Normally the above should work out of the box; I'm not sure why you experience all these problems. DS.> > Now please guide me as to how to evaluate some matlab expressions. > > Thanking you, > > > Regards > Bhanu Kalyan K > > > > Bhanu Kalyan K > BTech CSE Final Year > reach4kalyan at gmail.com > Tel :+91-9885238228 > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com
[Forwarding to r-help for completeness. /Henrik] ---------- Forwarded message ---------- From: Henrik Bengtsson <hb at stat.berkeley.edu> Date: Dec 28, 2006 9:45 PM Subject: Re: [R] Query regarding linking R with Matlab To: kalyansikha at yahoo.com Hi. On 12/28/06, Bhanu Kalyan.K <kalyansikha at yahoo.com> wrote:> Respected Sir, > I am sorry that i forgot to 'setVerbose'. Now when that is set, The result > is: > > > Matlab$startServer(minimize=FALSE) > [1] 0 > > > matlab <- Matlab(host="localhost") > > > open(matlab) > [1] TRUE > > > setVerbose(matlab, -2) > > > res <- evaluate(matlab, "A=1+2;", "B=ones(2,20);") > > Sending expression on the Matlab server to be evaluated...: 'A=1+2; > B=ones(2,20);' > Received an 'OK' reply (0) from the Matlab server. > Evaluated expression on the Matlab server with return code 0. > > > res > NULL > > > close(matlab) > Closing connection to host 'localhost' (port 9999)... > Received an 'OK' reply (0) from the Matlab server. > Closing connection to host 'localhost' (port 9999)...done > > What should "res" object contain finally when we are doing > res <- evaluate(matlab, "A=1+2;", "B=ones(2,20);") ??The return value of evaluate() is NULL when successful (I noticed the help is saying '0'; I've corrected that for the next version). Anyway, 'res' contains nothing of interest. If there is any error on the Matlab side, an error is thrown in R giving you the details. What you might want to do is: data <- getVariable(matlab, "A") str(data)> > once i do > close(matlab), the matlab server is shutdown,Yes, the server script is shut down, but Matlab is left running on purpose so you have a chance to continue working with the Matlab session by hand.> However, when i ask for the > values of A & B in matlab window, the output is displayed correctly. The > matlab window outputs thus: > ? A > > A > > 3 > > ? B > > B > > Columns 1 through 17 > > 1 1 1 1 1 1 1 1 1 1 1 1 > 1 1 1 1 1 > 1 1 1 1 1 1 1 1 1 1 1 1 > 1 1 1 1 1 > > Columns 18 through 20 > > 1 1 1 > 1 1 1 > > What do you interpret?That everything is now working as expected. See the example of help(Matlab) to get more ideas how to interact with Matlab from R. Cheers Henrik> Kindly let me know > > > Regards > Bhanu Kalyan K > > > Bhanu Kalyan K > BTech CSE Final Year > reach4kalyan at gmail.com > Tel :+91-9885238228 > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com