set Y-Axis to descending order without changing data

I am calling matalb from C++ using MatlabEngine.
I have a 2D array of data absCxRes and two vectors timeShift for x-axis and frq for y-axis
arma::vec frq = arma::linspace(src1.fs() / longChunk, 0, cxResMat.n_rows);
The Y-Axis starts with 6000 and goes down to 0. However, when the image is displayed, the Y-Axis values start with 0.
std::vector<matlab::data::Array> args6({
factory.createArray({timeShift.size(), 1}, timeShift.begin(), timeShift.end()),
factory.createArray({frq.size(), 1}, frq.begin(), frq.end()),
factory.createArray({ absCxRes.n_rows, absCxRes.n_cols }, absCxRes.begin(), absCxRes.end())
});
MLPtr->eval(u"figure");
MLPtr->feval(u"pcolor", args6);
MLPtr->eval(u"shading interp");
How to change the Y-Axis numbers so that they would start with 6000 and go up to 0 without flipping the matrix.
Thanks

 Accepted Answer

I am not certain what the code actually does.
It seems to me that there are two options —
set(gca, 'YDir','reverse')
yt = get(gca, 'YTick');
set(gca, 'YTickLabel',flip(yt))
Then, choose the result that does what you want.
x = 1:10;
y = rand(size(x));
figure
plot(x, y)
grid
title('Original')
figure
plot(x, y)
grid
set(gca, 'YDir','reverse')
figure
plot(x, y)
grid
yt = get(gca,'YTick');
set(gca, 'YTickLabel',flip(yt))
.

6 Comments

Thanks.
Could you help me on how to call these:
yt = get(gca,'YTick');
set(gca, 'YTickLabel',flip(yt))
From c++?
Thanks!
Sure!
Just put them after whatever plot calls you used to plot (what appear to me to be multiple spectrogram plots) in the axes they wer plotted in. Alternatively, get the axis handle for those axes, and use that as the first argument to the get and set calls.
For example, using my original code, that would be something like:
x = 1:10;
y = rand(size(x));
figure
plot(x, y)
grid
title('Original')
figure(8)
plot(x, y)
grid
title('Flipped Y-Labels')
hf = get(figure(8)); % Return Specific Figure Handle
Ax = hf.CurrentAxes; % Handle To Contained Axes
yt = Ax.YTick; % Original Y-Tick Values
Ax.YTickLabel = flip(yt); % Flipped Y-Tick Labels
I am not exactly certain how the spectrogram plots are created, however if they are created similarly to the way I created the example plot here, that should work. The commented lines here can be put anywhere in the code after ‘figure(8)’ is created.
.
Thanks! I have made a matlab function that flips the Y-axis and call it from C++. It works.
After Struggling a lot i could find this. And it really helped me. Thanks a lot.
My pleasure!
(A Vote for it would be appreciated!)

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!