MATLAB Figure Horizontal Precision

2 次查看(过去 30 天)
Alan
Alan 2014-9-26
I'm creating a figure with a large timescale in MATLAB. The numbers I'm using go down to milliseconds and the full range is in thousands of seconds.
By default, when I plot a figure, it's giving me scientific notation on the time with 4 decimal points, e.g. 4.400 and has x10^4 on the right. When I zoom in on the figure, the precision of the horizontal axis labels does not change. When I'm zoomed in really far, I get multiple labels all with the same number (e.g. 4.401, 4.401, 4.401, 4.401) so I can't tell the time difference in the points I'm looking at. How can I set the precision out to more decimal places?
I've tried:
xlbl = get(gca,'XTickLabel');
set(gca,'XTickLabel',sprintf('%16.6f',xlbl));
But all that does is add zeros the existing labels.
I tried
set(gca,'XTickLabel',sprintf('%6.6f',myarray(:,1))
where myarray(:,1) are the time values, but that freezes up because it's too many labels, and even if it worked it wouldn't give me what I want when I'm zoomed out.
format long
did nothing for the figure, only the command window.
Also, I need to be able to use the interactive Zoom in/Zoom out tools in the MATLAB figure window. If I use set(gca,'XTickLabel',....), even if I got it to display what I wanted, that's giving me fixed labels that have to be updated each time and would turn this into a science project.
Any suggestions?

回答(1 个)

Star Strider
Star Strider 2014-9-26
You likely need to get the 'XTick' values themselves, then use 'XTickLabel' to display them. This code works, but I don’t know if it will do what you want. Note that I divided the values by 1E+4 to eliminate formatting problems:
x = linspace(0, 2*pi*1E+4, 1000);
y = sin(x/1E+4)+cos(x/1E+4);
figure(1)
plot(x, y)
xtk = get(gca, 'XTick');
set(gca, 'XTickLabel', strsplit(sprintf('%10.7f ', xtk/1E+4)))
You will need the strsplit function to make it work correctly.
  2 个评论
Alan
Alan 2014-9-26
编辑:Alan 2014-9-26
Understood, you went straight to the values so it correctly shows all digits, and this would work for one static graph. Unfortunately, it keeps the exact same labels and does not rescale when I zoom in/out on the graph so it doesn't do what I need.
Star Strider
Star Strider 2014-9-26
I thought it did rescale when I tried it on my test code.
I don’t have your data so I can’t test it to see how to solve your problem specifically.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Exploration 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by