How to plot variable values on x axis?

8 views (last 30 days)
Cristina Magda
Cristina Magda on 18 Aug 2017
Commented: Adam on 18 Aug 2017
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 Comments
Adam
Adam on 18 Aug 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
Cristina Magda on 18 Aug 2017
I thought of that, but didn't knew how to do that automatically. Besides, every time it plots a new line it would start from the (0,0)point at the beginning and that's not how I want it. Or don't know how to to different.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 18 Aug 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 Comments
Cristina Magda
Cristina Magda on 18 Aug 2017
Edited: Cristina Magda on 18 Aug 2017
Works with plot(z,y) also. As I read about command numel - it counts all the values from that array. Then from 1 to that count, I get a number for each value, 1 2 3 4 ...And then plots the x number (1,2,3...) with value from Y. Tick labels now show 10,20,30...etc, but I don't want that. I want to show the maximum and minimum from x, like 0.2 then 12 then 0.2 again then -12 and so on. I have 132 values, need to show the min, max,0 and some points between. Doing that manually is out of the question.
Picture of what I've obtained.
Adam
Adam on 18 Aug 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 )

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!