필터 지우기
필터 지우기

Can you tell me why my x-axis grid in plot (b) shows incorrectly?

조회 수: 2 (최근 30일)
monkey_matlab
monkey_matlab 2015년 10월 21일
편집: dpb 2015년 10월 22일
Hello,
In my MWE below, I am plotting 4 graphs. I am trying to get graph (c) and (d) to show up with the x-axis as floating numbers, however I do not display the correct values for the x-axis points. If I put a break point in my program at line 23, and step through the program, it works without any problems. Can you tell me why I get an incorrect x-axis display if the program is run normally?
Here is my code:
clear;
clc;
x1 = linspace(100, 10^7, 100);
y1 = rand(100,1);
y2 = rand(100,1);
y3 = randi([-100,100],100,1);
subplot(2,2,1);
plot(x1, y1);
grid on;
title('(a) Silent Carrier (No Modulation)');
xlabel('Frequency (MHz)'); ylabel('Amplitude');
subplot(2, 2, 2);
plot(x1, y1);
grid on;
title('(b) Spectrum with Modulation');
xlabel('Frequency (MHz)'); ylabel('Amplitude');
subplot(2,2,3);
semilogx(x1, y2);
grid on;
title('(c) SSB Spectrum (Silent Carrier - NO Modulation)');
xlabel('Frequency Offset (Hz)'); ylabel('Phase Noise (dBc/Hz)');
curtick = get(gca, 'XTick');
set(gca, 'XTickLabel', cellstr(num2str(curtick(:))));
subplot(2,2,4);
semilogx(x1, y3);
grid on;
curtick = get(gca, 'XTick');
set(gca, 'XTickLabel', cellstr(num2str(curtick(:))));
title('(d) SSB Spectrum with Modulation');
xlabel('Frequency Offset (Hz)'); ylabel('Phase Noise (dBc/Hz)');
This is what I get for graphs (c) and (d):
and the "curtick" only has values [1 100000 10000000000.0000].
When it should be like:
I've recently found that if I leave the figure window open, in full screen, and re-run the program, it works. If I close the figure widow, and let it open up with the program, it comes in looking like the first figures.
  댓글 수: 2
Star Strider
Star Strider 2015년 10월 21일
편집: Star Strider 2015년 10월 21일
What do you want it to show? The full floating-point x-axis labels display correctly for me (although they run together). You can also use 'FontSize' in the set call to change the font size of the numbers in the x-ticks if you want to make them more readable, and in the more recent MATLAB releases, you can also rotate them.
(I would use the default exponent x-tick labels in this instance because of the magnitude of the tick labels in R2015b.)
monkey_matlab
monkey_matlab 2015년 10월 22일
Hello, I have modified my question with the output that I get. Thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2015년 10월 22일
HG engine determines in default size there isn't enough room for the tick labels so defaults to the three values. Tick labels are duplicated for the number of total ticks by reusing the same array over from the top which gives the result obtained.
Looks like a bit of an oversight internally; if I use the default figure size here w/ R2012b (HG1, not HG2), on this monitor there's only the three ticks initially. If I expand the window width it gets redrawn with 5 ticks but the tick labels don't get updated to match and I get your symptom.
The solution is to create the figure large enough to be able to display all those digits if that's what you want and/or set the tick marks and labels explicitly, don't rely on the default behavior for such an extreme case (I find trying to read that as is very difficult; I can't count the zeros to know just what the actual frequency is without a whole lot of effort).
Alternatively, as Star-S says, you might try setting the default font size smaller before the creation and see if that is taken into account in the calculations internally but still I think the solution is to set ticks and labels explicitly (altho again I'd agree w/ Star as noted above and go with the exponential notation here).
  댓글 수: 5
Star Strider
Star Strider 2015년 10월 22일
The objective is to display them as strings of floating-point numbers rather than in exponential notation. That's where the problem arises.
dpb
dpb 2015년 10월 22일
Yeah, I grok that; as noted you've got to have sufficient room either by changing the size of the figure or reducing the size of font or the font itself or combinations of any/all of the above.
Actually, I'll recant the bug and delete that comment; I hadn't thought thruough the OPs code quite thoroughly enough in that since he formatted and explicitly wrote the labels he had broken/severed the automagic link between the tick marks and tick labels on his own. I was thinking of the case when the tick labels are still being automagically drawn internally from the tick values.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by