Is possible to do an one line plot for structure array with different vector sizes
Show older comments
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 Comment
Stephen23
on 10 Apr 2020
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.
Answers (1)
Mehmed Saad
on 10 Apr 2020
Edited: Mehmed Saad
on 10 Apr 2020
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})
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!