How to plot the content of a Structure without using a for loop?

I need to plot the content of a structure fiel:
Each row in the column Position is made by 2 elements (x and y). I need to plot all the rows at once on a scatter plot.
Using:
pop(1).Position(1)
I can access the first value of the first row/column. But I would like to plot all the column values without using a for loop.
Is that possible?
Thanks in advance!

 採用された回答

Ahmet Cecen
Ahmet Cecen 2018 年 4 月 6 日
編集済み: Ahmet Cecen 2018 年 4 月 6 日

0 投票

Yes, in a way that is not apparent at first look, but very simple when you think about it:
[pop.Position]
I believe you can figure out the rest after this critical piece of information.

3 件のコメント

David Franco
David Franco 2018 年 4 月 6 日
It's not working...
For instance, if I use:
[pop(1).Position]
I get a 1x2 vector ([-0.4681 0.2270]) that is my (x,y) coordinates. If I plot this:
plot([pop(1).Position],'.r')
I get 2 points plotted...
If I try to plot all points at once:
plot([pop.Position(1)],[pop.Position(2)],'.r')
I get the error message:
Expected one output from a curly brace or dot indexing expression, but there were 50 results.
By the way, with your command ([pop.Position]) I get 50 answers, 1x2 each...
Ahmet Cecen
Ahmet Cecen 2018 年 4 月 6 日
編集済み: Ahmet Cecen 2018 年 4 月 6 日
Its just:
[pop.Position]
Do that and check the answer. A simple reshape will solve your problem after that.
Edit: Fixed Braces.
David Franco
David Franco 2018 年 4 月 6 日
It worked with your suggestion plus buffer function:
a = [pop.Position];
b = buffer(a,2)';
Because using just [pop.Position] a get an 1xN vector. That's why it wasn't working.
Many thanks!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by