Plotting Matrix points: 2

Given a matrix:
B
ans =
30.7805 28.7527 25.7110 23.6832 19.6268 17.5949 14.5547 11.5147 8.4725 5.4313
29.7667 27.7333 24.6923 21.6549 18.6114 15.5708 12.5285 9.4806 7.4582 4.4159
B.'
ans =
30.7805 29.7667
28.7527 27.7333
25.7110 24.6923
23.6832 21.6549
19.6268 18.6114
17.5949 15.5708
14.5547 12.5285
11.5147 9.4806
8.4725 7.4582
5.4313 4.4159
I am trying to plot the data as shown in this image:
I am trying to plot the 1st column as the orange points and the 2nd column as the blue points. The time between the points form a small box. The only axes I'm interested in is the X which will be composed of the overall time of the video. I am attempting to do this in a GUIDE axes. Eventually I want the user to be able to select the points and add labels.
Any advice in accomplishing this will be greatly appreciated!

 Risposta accettata

Paulo Silva
Paulo Silva il 20 Lug 2011
a =[30.7805 29.7667
28.7527 27.7333
25.7110 24.6923
23.6832 21.6549
19.6268 18.6114
17.5949 15.5708
14.5547 12.5285
11.5147 9.4806
8.4725 7.4582
5.4313 4.4159]
axis([0,max(a(:))+max(a(:))/5,0,2])
hold on
plot(a(:,1),1,'pb')
plot(a(:,2),1,'pr')
h=0.02;y=1;
arrayfun(@(n)rectangle('EdgeColor','y','Position',...
[min([a(n,1) a(n,2)]),y-h,abs(a(n,1)-a(n,2)),2*h]),1:size(a,1))
%with numbers if you want them:
arrayfun(@(n)text(min([a(n,1) a(n,2)]),y+2*h,...
num2str(size(a,1)-n+1)),1:size(a,1))

12 Commenti

B_Richardson
B_Richardson il 20 Lug 2011
wow... Theres alot going on here!
Fangjun Jiang
Fangjun Jiang il 20 Lug 2011
+1, Paulo! I admire, but, an overkill!
Paulo Silva
Paulo Silva il 20 Lug 2011
No big deal, I'm not even sure about the need of the arrayfun for the rectangle, I tried just with the rectangle but it didn't work.
Fangjun Jiang
Fangjun Jiang il 20 Lug 2011
I mean, what about axis([0,max(a(:))+max(a(:))/5,0,2]). I am scratching my head :).
Paulo Silva
Paulo Silva il 20 Lug 2011
max(a(:)) So the axis can catch all the action (plotted values)
just max(a) gives the row values where the maximum is so I put all elements from a in a column and max returns just one value
+max(a(:))/5 To give some space between plotted values and axis limit
Oleg Komarov
Oleg Komarov il 20 Lug 2011
After the stars to plot the rectangles:
x = [a(:,[2 2 1 1 2]).'; NaN(1,size(a,1))];
y = [1.02 .98 .98 1.02 1.02 NaN];
line(x(:).',repmat(y,1,size(a,1)),'Color','y')
Fangjun Jiang
Fangjun Jiang il 20 Lug 2011
Okay, Paulo. Thanks for clarification. I mis-read the parenthesis thus had a different guess. 1.2*max() might be easier on the eyes.
B_Richardson
B_Richardson il 21 Lug 2011
i'm still kinda lost...I see it works but I kinda need at least some understanding as to how it works. And just to clarify (and hopefully make it simpler) I dont really need the stars! lol I just put them there to clarify the starts and stops for you guys! (since at first nobody understood what I was trying to do!) I already have the data (start and stop). Whats more important was displaying the range (the box) of each start and stop.
Paulo Silva
Paulo Silva il 21 Lug 2011
it's simple, what don't you understand? did you check the functions on the MATLAB documentation?
Oleg Komarov
Oleg Komarov il 21 Lug 2011
My solution with line alone simply emulates a human hand. Take a piece of paper and try to draw a rectangle, you will start from upper-left, go one down, one right, one up, and one left to close the rectangle. Now you have to lift your hand and go to the next one, I use NaNs to break the line.
Try to visualize the input with a small "a", with say 2 starting/ending points and see what happens.
Fangjun Jiang
Fangjun Jiang il 21 Lug 2011
If you don't want the star, you just need to modify the plot() line. Help plot will show you different color, marker, and line style. Paulo uses rectangle() to draw the box. Oleg uses line() to draw the box. Mine uses the line width to fake the box. If you set the width bigger, it's pretty good. set(h,'LineWidth',8,'color','red')
B_Richardson
B_Richardson il 22 Lug 2011
I see. I took a break from the code (working on my research poster). I'm going to try and implement this in my final design next week. Thank you all for your help and consideration!

Accedi per commentare.

Più risposte (2)

Fangjun Jiang
Fangjun Jiang il 20 Lug 2011
B=[30.7805 28.7527 25.7110 23.6832 19.6268 17.5949 14.5547 11.5147 8.4725 5.4313
29.7667 27.7333 24.6923 21.6549 18.6114 15.5708 12.5285 9.4806 7.4582 4.4159];
h=line(B,zeros(2,10));
hold on;
plot(B(1,:),zeros(10,1),'y*');
plot(B(2,:),zeros(10,1),'bo');
set(h,'LineWidth',2,'color','red')

4 Commenti

B_Richardson
B_Richardson il 20 Lug 2011
Can you explain how this works?
B_Richardson
B_Richardson il 20 Lug 2011
I am also looking at your other example:
line(B',zeros(2,10));
hold on;
plot(B,zeros(10,2),'o');
But I cant get it to work?
Fangjun Jiang
Fangjun Jiang il 20 Lug 2011
Well, you changed the size of your B from previous one (10x2 vs. 2x10).
B_Richardson
B_Richardson il 20 Lug 2011
I see. I was confused as to how the plot function would accept the matrix. In reality, it doesnt matter to me whether it is 10 by 2 or 2 by 10. As long as the points are in the correct order (start/stop)

Accedi per commentare.

B_Richardson
B_Richardson il 20 Lug 2011

0 voti

I see that it works, but i'm trying to translate it into guide

3 Commenti

Jan
Jan il 20 Lug 2011
I do not see a reason to translate this to a complicated GUI, which would create the same code in the ideal case.
B_Richardson
B_Richardson il 20 Lug 2011
Im not sure if i understand? The matrix B is created in a GUI from user input. I am then, going to plot this data in axes in the GUI. Do this make sense?
Fangjun Jiang
Fangjun Jiang il 20 Lug 2011
Then, you'll probably have a button in your GUIDE GUI. In its callback function, you'll get the data B, create an axes and the use the above code to create the figure.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by