How can this Python code be written in Matlab? I'm trying to write a Matlab script that will allow two Stepper motors to scan in a Raster pattern but all I can find is this Python script. How the 'for' loop be replicated in Matlab?

15 vues (au cours des 30 derniers jours)
from pylab import *
# define some grids
xgrid = arange(20, 31)
ygrid = arange(10, 16)
xscan = []
yscan = []
for i, yi in enumerate(ygrid):
xscan.append(xgrid[::(-1)**i]) # reverse when i is odd
yscan.append(ones_like(xgrid) * yi)
# squeeze lists together to vectors
xscan = concatenate(xscan)
yscan = concatenate(yscan)
# quick plot
plot(xscan, yscan, '.-')
axis([19, 31, 9, 16])
show()
  4 commentaires
Rik
Rik le 16 Nov 2017
That is not the Matlab part of your problem. What is it you want to do? It looks like you want to plot some dotted line path. Is that the case? If so, what constraints are there?
David Sweeney
David Sweeney le 16 Nov 2017
What I'm trying to do in this code is start at some point within the grid say (0,0) then move a fixed amount of steps in the x direction until say (5,0) then take a step in the y direction to (5,1) then step backwards in the x direction to say (0,1). This will mimic the raster pattern I am trying to build. I've attached some Matlab code but it doesn't work the same as the above python code.
clear all
%initialise input parameters
ymin=0;
ystep=1;
ymax=1;
xmin=0;
xstep=1;
xmax=5;
x1=0;
for y = ymin:ystep:ymax
for x0=xmin:xstep:xmax
fprintf(' coordinates X,Y(%d,%d) \n' , x0, y);
end
if x0==xmax
for x1 = xmax:-xstep:xmin
fprintf('coordinates X,Y(%d,%d) \n' , x1, y);
end
end
end

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 17 Nov 2017
Modifié(e) : Rik le 17 Nov 2017
There is a Cody question that asked you to produce a matrix like the one below
X=[1 2 3;
6 5 4;
7 8 9];
My solution was a variation of the code below
x_num_steps=round((xmax-xmin)/xstep)+1;
y_num_steps=round((xmax-xmin)/xstep)+1;
sort_order=reshape(1:(x_num_steps*y_num_steps),x_num_steps,y_num_steps)';
sort_order(2:2:end,1:end)=sort_order(2:2:end,end:-1:1);
You can use this matrix to sort your coordinate grid (which you can generate with meshgrid). (You will need to transpose it to get the correct order.) [X,Y]=meshgrid(linspace(xmin,xmax,x_num_steps),linspace(ymin,ymax,y_num_steps)); %vectorize and sort X and Y sort_order=sort_order(:); X=X(:);Y=Y(:); X=X(sort_order);Y=Y(sort_order);
So, here all of the code:
%initialize variables
ymin=0;
ystep=1;
ymax=1;
xmin=0;
xstep=1;
xmax=5;
%calculate the number of steps in each direction (i.e. grid lines)
%this also catches the case where the gap isn't an integer multiple of the step size
x_num_steps=round((xmax-xmin)/xstep)+1;
y_num_steps=round((xmax-xmin)/xstep)+1;
%create a matrix with 1:total number of steps (grid points)
sort_order=reshape(1:(x_num_steps*y_num_steps),x_num_steps,y_num_steps)';
sort_order(2:2:end,1:end)=sort_order(2:2:end,end:-1:1);
sort_order=sort_order';
%sort_order contains the order in which we should go through the X and Y matrices
[X,Y]=meshgrid(linspace(xmin,xmax,x_num_steps),linspace(ymin,ymax,y_num_steps));
%vectorize and sort X and Y
sort_order=sort_order(:);
X=X(:);Y=Y(:);
X=X(sort_order);Y=Y(sort_order);
%plot the line and zoom out a bit to show the path
plot(X,Y,'--')
axis([xmin-xstep xmax+xstep ymin-ystep ymax+ystep])
  3 commentaires
David Sweeney
David Sweeney le 17 Nov 2017
Hi, could you add some comments to your code please? I'm having trouble deciphering it! I intend to use one stepper motor to move in the x direction and the other in the y direction, hence I need to know what variable I should be putting inside the 'move()' function. Thanks so much!

Connectez-vous pour commenter.

Plus de réponses (2)

Guillaume
Guillaume le 17 Nov 2017
What I'm trying to do in this code is start at some point within the grid say (0,0) then move a fixed amount of steps in the x direction until say (5,0) then take a step in the y direction to (5,1) then step backwards in the x direction to say (0,1).
What is the difficulty with that? And why use a loop for that (let alone two)?
xmin = 0; xmax = 5; xstep = 1;
ymin = 0; ymax = 1; ystep = 1;
%step 1: go from xmin to xmax, in xstep. y stays at ymin:
x = xmin:xstep:xmax;
y = repelem(xmin, numel(x));
xy = [x; y];
%step 2: go from ymin to max, in ystep. x stays where it is:
y = ymin+ystep:ystep:ystep;
x = repelem(xy(1, end), numel(y));
xy = [xy, [x; y]];
%step 3: go back to xmin. y stays constant
x = xmax-xstep:-xstep:xmin;
y = repelem(xy(2, end), numel(x));
xy = [xy, [x; y]]
  3 commentaires
Rik
Rik le 17 Nov 2017
You shouldn't try to do it with a loop. If you want to, you can still print either my solution or the solution by Guillaume, but why would you?
If you want a plot, you just use plot(X,Y,'--') with my solution, or a similar call with this solution.
David Sweeney
David Sweeney le 17 Nov 2017
Hi Guillaume, using your code would there be a way to finish the x routine (xmin to xmax in xstep increments) carry out the y routine (do a ystep) then do a -x routine (xmax to xmin) then do another y routine and so on until the full pattern is developed? when I change the ymax setting from 1 to 2 for example the results don't seem correct. I've attached the output when ymax is 10:
coordinates X,Y(0,0)
coordinates X,Y(1,0)
coordinates X,Y(2,0)
coordinates X,Y(3,0)
coordinates X,Y(4,0)
coordinates X,Y(5,0)
coordinates X,Y(6,0)
coordinates X,Y(7,0)
coordinates X,Y(8,0)
coordinates X,Y(9,0)
coordinates X,Y(10,0) Around this point it seems to do a y step after each xstep instead of a ystep after each x routine
coordinates X,Y(10,1)
coordinates X,Y(10,2)
coordinates X,Y(10,3)
coordinates X,Y(10,4)
coordinates X,Y(10,5)
coordinates X,Y(10,6)
coordinates X,Y(10,7)
coordinates X,Y(10,8)
coordinates X,Y(10,9)
coordinates X,Y(10,10)
coordinates X,Y(9,10)
coordinates X,Y(8,10)
coordinates X,Y(7,10)
coordinates X,Y(6,10)
coordinates X,Y(5,10)
coordinates X,Y(4,10)
coordinates X,Y(3,10)
coordinates X,Y(2,10)
coordinates X,Y(1,10)
coordinates X,Y(0,10)

Connectez-vous pour commenter.


Andrei Bobrov
Andrei Bobrov le 17 Nov 2017
Modifié(e) : Andrei Bobrov le 17 Nov 2017
[ii,jj] = ndgrid(20:30,10:15);
ii(:,2:2:end) = flip(ii(:,2:2:end));
plot(ii(:),jj(:));
axis([19, 31, 9, 16]);

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by