Programming and Graphics

(Kiana) #1

9.4 Transferring data to theMatlabdomain 305


//--- Matlab session:

engEvalString(cokar, "Handle1 = plot(xx,yy,’-’,’linewidth’,2);");
engEvalString(cokar, "hold on");

engEvalString(cokar, "axis square");
engEvalString(cokar, "axis([-5 5 -5 5])");
engEvalString(cokar, "set(Handle1,’EraseMode’,’xor’);");
engEvalString(cokar, "xlabel(’x’,’fontsize’,15)");
engEvalString(cokar, "ylabel(’y’,’fontsize’,15)");
engEvalString(cokar, "set(gca,’fontsize’,15)");

//--- Animation:

float velx=1.0;
float vely=0.5;
float Dt = 0.1; // time step

//--- Loop over time:

for (int istep=0; istep<=10000; istep++)
{
centerx = centerx+velx*Dt;
centery = centery+vely*Dt;

for (int i=0; i<=N; i++)
{
x[i]=x[i]+velx*Dt;
y[i]=y[i]+vely*Dt;
}

memcpy((void *)mxGetPr(xx), (void *)x, sizeof(x));
engPutVariable(cokar, "xx", xx);

memcpy((void *)mxGetPr(yy), (void *)y, sizeof(y));
engPutVariable(cokar, "yy", yy);

engEvalString(cokar, "set(Handle1,’XData’,xx,’YData’,yy);");
engEvalString(cokar, "drawnow;");

if(centerx > 5.0) velx=-velx; //--- reflect if necessary
if(centerx <-5.0) velx=-velx;
if(centery > 5.0) vely=-vely;
if(centery <-5.0) vely=-vely;
}

//--- End the session:
Free download pdf