How to plot variable values on x axis?
Afficher commentaires plus anciens
Ok, so I didn't knew how exactly ask in one sentence what I need. I have Y data that is like: y=[0:1:positive-value positivevalue-1:-1:0 0:-1:negative-value negative-value+1:1:0] Thus resulting in a range of values. The X data is function of Y, thus resulting in same number of values and same character increasing and decreasing. A little example (I have more values then that):
y=[0 1 2 3 4 3 2 1 0 0 -1 -2 -3 -4 -3 -2 -1 0]
X=[0.2 1 6 9 12 9 6 1 0.2 0.2 -1 -6 -9 -12 -9 -6 -1 0.2]
Now I need to plot x and y. I could use line(x,y) or plot(x,y). But when I do that the resulting graph is a line (see graph below), because x data falls back on the previous values. I need that X data to be raising from 0 to max value and then back to 0, then from 0 to min value and back to 0. Thus the Y data plotting over X would be a jigsaw or something like that. Or we could say I would need a graph looking like x=sin(y) (like in second figure, only x would have different values getting from 0 to max and back to 0). Is that even possible? Thank you.


2 commentaires
Adam
le 18 Août 2017
You should be able to just plot it as a new line (or plot) each time there is a change of direction. A bit fiddly, but simple enough to locate those change points.
Cristina Magda
le 18 Août 2017
Réponses (1)
Star Strider
le 18 Août 2017
The easiest way:
y=[0 1 2 3 4 3 2 1 0 0 -1 -2 -3 -4 -3 -2 -1 0];
x=[0.2 1 6 9 12 9 6 1 0.2 0.2 -1 -6 -9 -12 -9 -6 -1 0.2];
z = 1:numel(x);
figure(1)
plot3(z, x, y)
grid on
view(0,0)
2 commentaires
Cristina Magda
le 18 Août 2017
Modifié(e) : Cristina Magda
le 18 Août 2017
Adam
le 18 Août 2017
You can change the Tick Labels on the x axis, converting your x array (or a subset of it) to strings and then change the ticks to the same indices into z.
e.g. if you want the 1st, 5th, 9th, 12th values as your ticks, then you can just edit the
XTick
property to be z( [1 5 9 12] )
and
XTickLabel
to be
arrayfun( @num2str, x( [1 5 9 12] ), 'UniformOutput', false )
Catégories
En savoir plus sur Grid Lines, Tick Values, and Labels dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
