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?
显示 更早的评论
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 个评论
Rik
2017-11-16
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
2017-11-16
Rik
2017-11-16
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
2017-11-16
采纳的回答
更多回答(2 个)
Guillaume
2017-11-17
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 个评论
David Sweeney
2017-11-17
Rik
2017-11-17
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
2017-11-17
Andrei Bobrov
2017-11-17
编辑:Andrei Bobrov
2017-11-17
[ii,jj] = ndgrid(20:30,10:15);
ii(:,2:2:end) = flip(ii(:,2:2:end));
plot(ii(:),jj(:));
axis([19, 31, 9, 16]);
类别
在 帮助中心 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!