3

In Matlab

xlim

returns e.g. [0,1]. Now I want to access the last value of xlim directly, therefore without storing the xlim return values in a variable. Is that possible?

xlim(2)

is obviously interpreted as a function call, not as getting the value at index 2.

Any workaround on this, except the following, which involes an additional variable?

temp = xlim;
temp(2)
Amro
  • 123,847
  • 25
  • 243
  • 454
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
  • 2
    See [this question and answer](http://stackoverflow.com/q/3627107/1214731). Personally, I like to use the anonymous function route if I need something like this. – tmpearce Jun 22 '12 at 20:44

1 Answers1

3

Since xlim always returns an ordered pair (as long as xdir is not set to reverse) , you can access the elements directly using the min and max functions.

Use min(xlim) to access the first element and max(xlim) for the second element.

If xdir is set to reverse, max(xlim) will return the first element and min(xlim) the second.

H.Muster
  • 9,297
  • 1
  • 35
  • 46
  • 1
    although clever, it it less readable than just using a temp variable. You get my vote anyway +1 – Amro Jun 22 '12 at 22:38
  • I agree on the lack of readability and also would prefer the temp variable. I just noticed the trick and wanted to share it since it meets the request of the questioner. – H.Muster Jun 23 '12 at 12:18