Reputation: 742
I have some matrix with n rows and 2 columns. I want to plot the values from the second column as a function of those from the first one. The problem is that the values from the first column begins with some greater values which decrease, e.g. 5 4 3 2 1 Therefore, when I plot the function the graph is automatically flipped i the way that 1 is on the left. How to avoid this flipping? I know that I could invert the graph, but I'd like to have an universal solution, which works also for datasets where the column starts with smaller numbers.
Upvotes: 0
Views: 36
Reputation: 2565
After plotting the data of your matrix M
, add the following condition:
if(~issorted(M(:, 1)))
set(gca, 'Xdir', 'reverse');
end
The function issorted
will check if the first column is sorted in ascending order, and if it's not, then the x-axis of your plot will be reversed.
Upvotes: 2