# Plot all polyshapes in cell array

조회 수: 7 (최근 30일)
Luis Rosety 2022년 5월 7일
댓글: Steven Lord 2022년 5월 8일
Hello,
I am learing Matlab and I would like to plot all polyshapes stored in a cell array (D) but avoiding for...end loop.
Each cell contains a 2d vector with each vertex coordinates.
So far I only know to plot it like this
for i=size(D,1)
plot(D{i}(:,2), D{i}(:,1));
end
Is there any more concise way to achieve it with a single instruction not using a for...end loop?
Thanks

댓글을 달려면 로그인하십시오.

### 채택된 답변

Voss 2022년 5월 7일
You could use cellfun:
cellfun(@(x)plot(x(:,1),x(:,2)),D);
That's more concise, I guess, but it also may be less clear than the loop you already have. And it's not likely to be much different performance-wise.
[In general, cellfun(f,C) runs the function f on each cell of cell array C. In this case, the function is the anonymous function
@(x)plot(x(:,1),x(:,2))
and the cell array is D.]
##### 댓글 수: 2없음 표시없음 숨기기
Luis Rosety 2022년 5월 8일
편집: Luis Rosety 2022년 5월 8일
Thanks!
This is what I was looking for: A single command.
Voss 2022년 5월 8일
You're welcome!

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Steven Lord 2022년 5월 8일
If you've already created them in a cell array you can simply turn that cell array into an array of polyshape objects and plot that array. Or you could start with a polyshape array initially, filling it in as you go.
C = cell(1, 10);
A1 = repmat(polyshape, 1, 10);
for k = 1:10
N = nsidedpoly(1000, 'Center', [2*k 0], 'Radius', 0.75);
C{k} = N; % Create a cell
A1(k) = N; % Fill in an array
end
A = [C{:}]
A =
1×10 polyshape array with properties: Vertices NumRegions NumHoles
plot(A)
axis equal
grid on
A1
A1 =
1×10 polyshape array with properties: Vertices NumRegions NumHoles
figure
plot(A1)
axis equal
grid on
##### 댓글 수: 2없음 표시없음 숨기기
Luis Rosety 2022년 5월 8일
This doesn't work in my case.
It throws an error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Steven Lord 2022년 5월 8일
Ah, I went off the subject of the message. You don't actually have polyshape objects in a cell array, you have coordinate data in the cell array. In that case, a skeleton for the approach I'd use:
% C is the cell array of coordinate data
P = repmat(polyshape, size(C)); % One polyshape per cell in C
for k = 1:numel(C)
data = C{k};
P(k) = polyshape(data(:, 2), data(:, 1));
end
plot(P)
I'm assuming the data in the cell array matches the requirements the polyshape function has for its inputs.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by