How to plot rectangles using variables

5 次查看(过去 30 天)
I'm trying to plot several rectangles with data from an array, and i can't quite figure out how to plot this way
The code i've been trying:
T = testarray;
x1 = T(1:216,1);
y1 = T(1:216,2);
w1 = T(1:216,3);
h1 = T(1:216,4);
x = table2array(x1);
y = table2array(y1);
w = table2array(w1);
h = table2array(h1);
rectangle('Position',[x(1:216,1) y(1:216,1) w(1:216,1) h(1:216,1)])
axis([-1 2 -1 2])
Any idea what i'm doing wrong? I keep getting the error "Error using rectangle
Value must be a 4 element vector"

采纳的回答

the cyclist
the cyclist 2022-10-30
编辑:the cyclist 2022-10-30
Are you trying to plot 216 rectangle with that one line? If you look at the documentation, you will see that you cannot do that. It is expecting a single 4-element vector, not an array.
You need to loop over your arrays. (If that is, indeed, what you are trying to do.) Here is how you can do that. I just used 6 rectangles, and pretend data.
N = 6;
testarray = array2table(rand(N,4));
T = testarray;
x1 = T(1:N,1);
y1 = T(1:N,2);
w1 = T(1:N,3);
h1 = T(1:N,4);
x = table2array(x1);
y = table2array(y1);
w = table2array(w1);
h = table2array(h1);
figure
hold on
for nr = 1:N
rectangle('Position',[x(nr) y(nr) w(nr) h(nr)])
axis([-1 2 -1 2])
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by