How to plot rectangles using variables

5 Ansichten (letzte 30 Tage)
Nathaniel
Nathaniel am 30 Okt. 2022
Kommentiert: Nathaniel am 31 Okt. 2022
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"

Akzeptierte Antwort

the cyclist
the cyclist am 30 Okt. 2022
Bearbeitet: the cyclist am 30 Okt. 2022
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
  1 Kommentar
Nathaniel
Nathaniel am 31 Okt. 2022
Thank you so much, that works!
Sorry for the inconvienience

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by