Is possible to do an one line plot for structure array with different vector sizes

Hello there,
First of all I am using R2020a
I usually organize my data input in a structure array and then is fairly easy to plot if all I have are integers/doubles by simply doing a
plot(data.x, data.y)
with data being something like (in parenthesis is the type of the variable)
data =
1×5 struct array with fields:
reference (str)
x (integer)
y (double)
w (double)
z (double)
l (double)
Now if I want to plot a data with the following format
data =
1×5 struct array with fields:
reference (str)
x (vector of integers)
y (vector of doubles)
w (double)
z (double)
l (double)
where the vector can alternate between 1x3 to 1x8, with length(data(i).x) = length(data(i).y).
I know I could create a for loop having
for i = 1:length(data)
plot(data(i).x, data(i).y)
end
But I was wondering if there was a way to do this in one line and if there is, how.
Thanks!

1 件のコメント

Your first example does not make sense. Given that structure array
data =
1×5 struct array with fields
x (integer)
y (double)
...
and then calling
plot(data.x, data.y)
is exactly equivalent to
plot(data(1).x, data(2).x,... data(5).x, data(1).y, data(2).y,... data(5).y)
which is unlikely to be a very useful plot.

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

回答 (1 件)

Mehmed Saad
Mehmed Saad 2020 年 4 月 10 日
編集済み: Mehmed Saad 2020 年 4 月 10 日
1- The long method
dc = struct2cell(data);
x = squeeze(dc(1,1,:));% as x is the 1st field
y = squeeze(dc(2,1,:));% as y is the 2nd field
figure,hold on,cellfun(@plot,x',y')
2-The short method(1 line)
figure,hold on,cellfun(@plot,{data.x},{data.y})

製品

質問済み:

2020 年 4 月 10 日

コメント済み:

2020 年 4 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by