1

My goal: plot a horizontal line with a square bracket (---]) end to it.

I usually plot horizontal lines with

line([0,1],[2,2],'linestyle',':')

I can add usual markers at the end by

plot([0,1],[2,2],'o')

but not square bracket.

Any suggestions?

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Amir Sagiv
  • 376
  • 5
  • 23
  • Sorry for that but what is your question? add square brackets to which command and what argument location????? – Abo Lregal Feb 19 '17 at 11:40
  • 4
    @AboLregal The question is perfectly clear, he wants to add a square bracket *in a plot* as a marker. Also please dial down on the usage of question marks, one is more than enough to show that you have a question. – Adriaan Feb 19 '17 at 11:53
  • 1
    A possible, but probably computationally more heavy than necessary, can be found [in this answer](http://stackoverflow.com/a/36070755/5211833): overlay and image of a square bracket at the end of the line. – Adriaan Feb 19 '17 at 12:03
  • @Adriaan thanks, but I'm still hopefull for something easier. If all else fail, then this will be the solution... – Amir Sagiv Feb 19 '17 at 12:05
  • it can't except the color because matlab deal with name and value as strings – Abo Lregal Feb 19 '17 at 15:03
  • rgb in markercolor – Abo Lregal Feb 19 '17 at 16:53
  • @AboLregal first off, please use the `@username` syntax when replying to people, so that they actually see your comment. As to the contents: there's no mention of colour here, so what does that have to do with the question? – Adriaan Feb 19 '17 at 16:54

2 Answers2

5

Here's a terrible hack that kinda achieves what you want:

XVALS = [0,1; 0,2; 0,3].';
YVALS = [3 3; 2,2; 1,1].';
INVIZ_OFFSET = 0.04;
figure(); 
% Step 1: Plot squares:
plot(XVALS(2,:), YVALS(2,:),'bs');
% Step 2: Plot invisible squares:
hold on;
plot(XVALS(2,:)-INVIZ_OFFSET, YVALS(2,:),'ws','MarkerFaceColor','w');
% Step 3: Plot lines
plot(XVALS, YVALS,':b');

% Play with limits:
axis image; xlim([0,5]); ylim([0,4]);

Result:

enter image description here

The idea is that a "bracket" marker can be obtained using an obscured square marker. Obviously this isn't suitable for all plots, but I think you can work from here...

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
3

I think your simplest option would be the text command:

l = line([0,1],[2,2],'linestyle',':');
text(l.XData(end),l.YData(end),']','VerticalAlignment','middle',...
    'FontSize',12,'FontWeight','bold','Color',l.Color)

You can go further and add a rotation:

x = 0:0.1:0.5*pi;
p = plot(x,cos(x)+1.5,'--r');
text(p.XData(end),p.YData(end),']','VerticalAlignment','middle',...
    'Rotation',atand(diff(p.YData(end-1:end))/diff(p.XData(end-1:end))),...
    'FontSize',12,'FontWeight','bold','Color',p.Color)

The rotation is not perfect, but it's a good start. Here is the result:

bracket end


EDIT:

For a pre-2014b version of Matlab you will need to use the get function:

l = line([0,1],[2,2],'linestyle',':');
x = get(l,'XData');
y = get(l,'YData');
text(x(end),y(end),']','VerticalAlignment','middle',...
      'FontSize',12,'FontWeight','bold','Color',l.Color)
EBH
  • 10,350
  • 3
  • 34
  • 59
  • thanks for the answer. However, when I type the `l=line(...` command I get just a `double` for `l`, which doesn't have any fields or a structure to it. Any thoughts on why's that? – Amir Sagiv Feb 21 '17 at 08:54
  • @AmirSagiv that's strange. Do you get the line plotted? maybe you have a variable called `line`? Try to run this in a fresh session of Matlab. Alternatively, replace `line` with `plot`, as in the second example above (you can keep the same input). – EBH Feb 21 '17 at 12:43
  • Still the same with `plot` in a fresh session. Is this a relatively new feature of Matlab? I'm running a 2013a. – Amir Sagiv Feb 23 '17 at 10:35
  • 1
    @AmirSagiv Well, now it's clear. You cannot use the `.` notation for graphic handles before 2014b. I have edited my answer for previous versions. – EBH Feb 23 '17 at 12:15