[R] Query regarding linking R with Matlab

Henrik Bengtsson hb at stat.berkeley.edu
Wed Dec 20 12:00:16 CET 2006


Hi.

On 12/20/06, Bhanu Kalyan.K <kalyansikha at yahoo.com> wrote:
>
> 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.

The R.matlab package basically allows you to *evaluate* Matlab code
sent from R as text strings.  In addition you can transfer *data
structures* between R and Matlab (in both directions).  So, instead of
running Matlab in one window and R in another, typing some commands in
Matlab, saving data to file, moving over to R and load that data file
into R etc, the R.matlab package allows you to do all that from within
R.  That is the main idea behind R.matlab.  Thus, you cannot generate
graphs in Matlab and magically expect them to be transferred to R.
However, like when you run Matlab "by hand" you can save graphs to
file, e.g. PNG or EPS files.  The R.matlab interface allows you to
tell Matlab to do that from within R.

To run (=evaluate) your 'swissroll.m' Matlab script from R

matlab <- Matlab(host="localhost", port=9998)
if (!open(matlab))
  throw("Matlab server is not running: waited 30 seconds.")
res <- evaluate(matlab, "swissroll")

This will then open *Matlab* windows displaying the figures.

You can import the Matlab variables of interest to R by:

vars <- getVariable(matlab, c("Y", "X", "K", "d"))

...and the work with the variables in R instead, e.g. Y <- vars$Y.

Hope this helps

Henrik

>
> regards,
> Bhanu Kalyan K
>
>
>
> Bhanu Kalyan K
>   BTech CSE Final Year
>   reach4kalyan at gmail.com
>   Tel :+91-9885238228
>
>  __________________________________________________
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list