set xticks by calculation but also force 0 to be displayed

15 views (last 30 days)
I am setting xticks manually based on user inputs for distance. The plot will span the distance range; for example -1.3E-06 to 1.3E-06. The calculation works well to calculate the distance between tick marks, but depending on how it's done it may or may not automatically included 0 on the x axis. Is there a way I can force 0 to be displayed on the axis without altering my method for calculating the distance between tick marks.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Jun 2021
format long g
%demonstration values
x = linspace(-1.4e-6,1.32e-6,29)
x = 1×29
-1.4e-06 -1.30285714285714e-06 -1.20571428571429e-06 -1.10857142857143e-06 -1.01142857142857e-06 -9.14285714285714e-07 -8.17142857142857e-07 -7.2e-07 -6.22857142857143e-07 -5.25714285714286e-07 -4.28571428571428e-07 -3.31428571428571e-07 -2.34285714285714e-07 -1.37142857142857e-07 -3.99999999999998e-08 5.71428571428574e-08 1.54285714285714e-07 2.51428571428572e-07 3.48571428571429e-07 4.45714285714286e-07 5.42857142857143e-07 6.4e-07 7.37142857142857e-07 8.34285714285715e-07 9.31428571428571e-07 1.02857142857143e-06 1.12571428571429e-06 1.22285714285714e-06 1.32e-06
y = sinpi(2e6*x)
y = 1×29
-0.587785252292474 -0.945356108439569 -0.961536119504149 -0.630482294044714 -0.0717461367613788 0.512899277405907 0.912324417231455 0.982287250728688 0.697522514745702 0.160865609613698 -0.433883739117559 -0.871947226253863 -0.995129582585911 -0.758946694294555 -0.248689887164854 0.351374824081344 0.824549628907141 0.999959716170871 0.814260281314378 0.334511859971844 -0.266036845566676 -0.770513242775789 -0.996738762088015 -0.863017923326349 -0.417640539972131 0.178556894798636 0.710273137068058 0.985492653565727 0.904827052466019
plot(x,y)
array_of_tick_values = -1.3e-6:.24e-6:1.3e-6
array_of_tick_values = 1×11
-1.3e-06 -1.06e-06 -8.2e-07 -5.8e-07 -3.4e-07 -1e-07 1.4e-07 3.8e-07 6.2e-07 8.6e-07 1.1e-06
%force a 0
xticks(union(array_of_tick_values, 0))
However, this could result in two ticks being close together. For example if your calculated tick was at -1e-10 then you would get a tick at -1e-10 and at 0. In the above example plot you can see that the -0.1 tick is close to the 0 tick
Whereas...
plot(x,y)
xtrange = max(abs(array_of_tick_values));
array_of_tick_values = linspace(-xtrange,xtrange,length(array_of_tick_values));
xticks(array_of_tick_values)
  2 Comments
Walter Roberson
Walter Roberson on 10 Jun 2021
Hmmm I just realized that you need the linspace length to be odd to force going through 0.
Robert Demyanovich
Robert Demyanovich on 10 Jun 2021
That is true. I tried your linspace solution and it didn't work. But the union solution did and that is good enough for me.

Sign in to comment.

More Answers (1)

Joel Lynch
Joel Lynch on 10 Jun 2021
Edited: Joel Lynch on 10 Jun 2021
set(gca,XTick,array_of_values)
  2 Comments
Robert Demyanovich
Robert Demyanovich on 10 Jun 2021
Okay, so I created an vector of values which has the tick marks. How can i test if the vector_of_values has a value of zero and if not, insert a column with the value of zero in the correct place.
Walter Roberson
Walter Roberson on 10 Jun 2021
ismember(0, vector_of_values)
but that leaves the searching open in that form. But if the vector is sorted you could
idx = find(vector_of_values <= 0, 1, 'last')
if vector_of_values(idx) == 0
exact match for 0
else
vector_of_values = [vector_of_values(1:idx), 0, vector_of_values(idx+1:end)]
end
assuming row vector
... But the union() approach I posted is much more compact and has the same effect.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!