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"
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
