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?
Mostrar comentarios más antiguos
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 comentarios
Rik
el 16 de Nov. de 2017
I have never worked with Python, so I can't translate this to Matlab. If you explain your original problem, people might be able to help you.
(This looks like it is relatively basic Python. Have you tried looking up the documentation for these functions?)
David Sweeney
el 16 de Nov. de 2017
Rik
el 16 de Nov. de 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
el 16 de Nov. de 2017
Respuesta aceptada
Más respuestas (2)
Guillaume
el 17 de Nov. de 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 comentarios
David Sweeney
el 17 de Nov. de 2017
Rik
el 17 de Nov. de 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
el 17 de Nov. de 2017
Andrei Bobrov
el 17 de Nov. de 2017
Editada: Andrei Bobrov
el 17 de Nov. de 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]);
Categorías
Más información sobre Call Python from MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!