gpt4 book ai didi

matlab获取实际值 'xTick'

转载 作者:行者123 更新时间:2023-12-02 03:34:06 25 4
gpt4 key购买 nike

我正在使用 Matlab 绘制数据并尝试处理刻度/刻度标签。

我打算做什么

绘制带有动态刻度标签的图表,这样我就可以找出图表上特定点的时间戳。因此时间戳 (xTickLabel) 应该只来自数据源。

我取得了什么

现在我可以:

用动态刻度标签绘制图表,但标签不够精细:这些标签是每个刻度一秒。

下面是绘图代码:

fig = figure;
x = M(:, 1);

% convert the x (time) value from unix time to matlab time
x = (x / 86400 / 1000) + datenum(1970,1,1);
y1 = M(1:end, 2);
y2 = M(1:end, 3);
y3 = M(1:end, 4);

plot(x, y1, x, y2, x, y3);

xlim([x(1), x(end)]);

set(gca, 'XTick', x);
grid on;

datetick('x','yyyy-mm-dd HH:MM:SS.FFF', 'keeplimits', 'keepticks');
xticklabel_rotate;

hleg1 = legend('x', 'y', 'z');
% zoomAdaptiveDateTicks('on');

代码运行良好,可以在不启用 zoomAdaptiveDateTicks('on') 的情况下绘制来自源数据的每个时间戳的图形,原始图形显示如下:

matlab1

并且如果启用了 zoomAdaptiveDateTicks('on'),则图表将具有动态刻度标签,但无论缩放比例如何,标签都是每个刻度一秒。

matlab2

我的问题

如何制作具有足够精细和动态刻度标签的图表?

我试图找出刻度标签的值,但没有成功,还有其他方法吗?

谢谢,

为了您的引用,这里是 zoomAdaptiveDateTicks( link )

的代码
 function zoomAdaptiveDateTicks(varargin)
% ZOOMADAPTIVEDATETICKS - Make date ticks adapt to zooming
%
% zoomAdaptiveDateTicks('on')
% Turns on the automatic adaptation of date ticks
% to user zooming for the current figure window
%
% zoomAdaptiveDateTicks('off')
% Turns off the automatic adaptation of date ticks
% to user zooming for the current figure window
%
% zoomAdaptiveDateTicks('demo')
% Opens a demo figure window to play with


if (nargin>0)
switch varargin{1}
case 'demo'
% Create demo values
dates = floor(now) - linspace(1169,0,15000)';
values= randn(15000,1);
% Show data with date ticks
figure
plot(dates,values)
datetick('x')
zoomAdaptiveDateTicks('on')
case 'on'
% Define a post zoom callback
set(zoom(gcf),'ActionPostCallback', @adaptiveDateTicks);
case 'off'
% Delete the post zoom callback
set(zoom(gcf),'ActionPostCallback', '');
otherwise
figure(gcf)
end
end


function adaptiveDateTicks(figureHandle,eventObjectHandle)

% Resetting x axis to automatic tick mark generation
set(eventObjectHandle.Axes,'XTickMode','auto')

% get and set the tick length
% a = get(eventObjectHandle.Axes, 'XTick');
% minimum = min(a);
% maximum = max(a);

fprintf('minimum :%s, maximum:%s \n', num2str(minimum), num2str(maximum));
% set(eventObjectHandle.Axes, 'XTick'
%set(eventObjectHandle.Axes, 'TickLength', 0.5*(get(eventObjectHandle.Axes, 'TickLength')))

% using automaticallly generate date ticks
datetick(eventObjectHandle.Axes,'x','keeplimits')

如果我没有把问题解释清楚,请随时提问。

最佳答案

问题似乎出在 xticklabel_rotate 上,因为它卡住了标签。所以我想也许我应该在回调中做这样的事情:

   set(gca,'XTickLabelMode','auto');
xticklabel_rotate;

但这没有用...然后我注意到 xticklabel_rotate.m 中的第 35 行:

% Note : you can not RE-RUN xticklabel_rotate on the same graph.

你应该试试 rotateXLabels而不是 xticklabel_rotate


根据 ss1271 的评论,以下代码能够达到预期的结果。所需的修改是将 zoomAdaptiveDateTicks('on')xticklabel_rotate 替换为 rotateXLabels(gca, 90):

close all force; clear variables; clc;

M = [(1:1000)',randn(1000,3)]; ...' //This is just to fix SO formatting

fig = figure(1337);
x = M(:, 1);

% convert the x (time) value from unix time to matlab time
x = (x / 86400 / 1000) + datenum(1970,1,1);
y1 = M(:, 2);
y2 = M(:, 3);
y3 = M(:, 4);

plot(x, y1, x, y2, x, y3);

xlim([x(1), x(end)]);

set(gca, 'XTick', x);
grid on;

datetick('x','yyyy-mm-dd HH:MM:SS.FFF','keepticks','keeplimits');

rotateXLabels(gca, 90);

hleg1 = legend('x', 'y', 'z');

关于matlab获取实际值 'xTick',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24862443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com