フィルターのクリア

Plot all polyshapes in cell array

7 ビュー (過去 30 日間)
Luis Rosety
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
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
Luis Rosety 2022 年 5 月 8 日
編集済み: Luis Rosety 2022 年 5 月 8 日
Thanks!
This is what I was looking for: A single command.
Voss
Voss 2022 年 5 月 8 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
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
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
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 Center および File ExchangeElementary Polygons についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by