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!