I am receiving the following error probably referring to 'XLim' or 'YLim' as 'axes' parameters:
Error using axes
Value must be a 1x2 vector of numeric type in which the second element is larger than the first and may be Inf
I checked my workspace and everything seems fine since 'XLim' is a 1x2 datetime vector and 'YLim' is a 1x2 numeric vector with their 'min limit' < 'max limit'.
The related code is the following:
ax1 = gca;
ax1_Pos = ax1.Position;
ax1_XLim = ax1.XLim;
ax1_YLim = ax1.YLim;
ax1_start = ax1.XLim(1);
%ax1_end = ax1.XLim(2);
xticks([ax1_start, time_dat(51-1), time_dat(86-1), time_dat(129-1), time_dat(170-1)]);
ax2_labels = {'z_acc_test','setup1','y_acc_test','setup2','x_acc_test'};
ax2 = axes('Position', ax1_Pos, 'XLim', ax1_XLim, 'YLim', ax1_YLim, 'XAxisLocation', 'top', 'YAxisLocation', 'right', 'XTick', xticks, 'XTickLabel', ax2_labels);

 채택된 답변

Adam Danz
Adam Danz 2018년 7월 12일
편집: Adam Danz 2018년 7월 12일

1 개 추천

In the last line of your code, what does 'xticks' equal? I see you use the function xticks() but I don't see where you have assigned a value to a variable 'xticks'. Could your error be due to that?

댓글 수: 11

So to be fair that was a problem of the algorithm that I have now managed to solve (see below) but apparently it wasn't this issue that was triggering the above mentioned error since it's still active. The new version of the code is:
ax1 = gca;
ax1_Pos = ax1.Position;
ax1_XLim = ax1.XLim;
ax1_YLim = ax1.YLim;
ax1_start = ax1.XLim(1);
%ax1_end = ax1.XLim(2);
xticks([ax1_start time_dat(51-1) time_dat(86-1) time_dat(129-1) time_dat(170-1)]);
xt = xticks;
%xticks('manual');
ax2_labels = {'z_acc_test','setup1','y_acc_test','setup2','x_acc_test'};
ax2 = axes('Position', ax1_Pos, 'XLimMode', 'manual', 'XLim', ax1_XLim, 'YLim', ax1_YLim, 'XAxisLocation', 'top', 'YAxisLocation', 'right','XTickMode', 'manual', 'XTick', xt, 'XTickLabel', ax2_labels);
Adam Danz
Adam Danz 2018년 7월 12일
Could you provide the values of ax1_XLim and ax1_YLim and/or post a copy of your plot?
ax1_XLim = ['Jul 11, 2018, 15:52' 'Jul 11, 2018, 16:01']
ax1_YLim = [-1.50000000000000 1.50000000000000]
The plot can't be generated with this particular code due to the error
Adam Danz
Adam Danz 2018년 7월 12일
편집: Adam Danz 2018년 7월 12일
This should work
ax1_XLim = {'Jul 11, 2018, 15:52', 'Jul 11, 2018, 16:01'}; %Note brackets!
xl = datenum(ax1_XLim);
xlim(xl)
or
axes(..., 'XLim', xl, ...)
Thanks Adam for the proposal but I've added the brackets as a means of representing this data, although in reality they don't exist. ax1_XLim it is a 1x2 datetime vector containing the data I showed you above. To confirm that my command windows outputs this:
ax1_XLim =
1×2 datetime array
Jul 11, 2018, 15:52 Jul 11, 2018, 16:01
Vasileios Leivadas
Vasileios Leivadas 2018년 7월 12일
I am actually wondering why the error specifies on just a 'numeric' vector and not other eligible types.
Adam Danz
Adam Danz 2018년 7월 13일
편집: Adam Danz 2018년 7월 13일
My code should still work whether ax1_XLim is a cell array of strings (my example) or a datetime vector (your data). Then you can use datetick() to format the tick labels in string format.
datetick(ax1, 'x', 'mmm dd, yyyy HH:MM')
I suppose matlab doesn't consider the datetime class as numeric which is why it needs converted using datenum().
Hi Adam! I tried to 'datenum' the 'ax1_XLim' vector and it actually converted it to a 'double' type which is compatible with the 'XLim' parameter. Only problem is that the double precision trait is not enough to distinguish the two limits and therefore they are represented as equal. Hence and the following error:
Value must be a vector of type single or double whose values increase
Adam Danz
Adam Danz 2018년 7월 13일
편집: Adam Danz 2018년 7월 13일
I see, what are the datetime limits you're using? The example you provided earlier works for me.
If you mean the actual values, they are the following:
'Jul 11, 2018, 15:52' 'Jul 11, 2018, 16:01'
Adam Danz
Adam Danz 2018년 7월 13일
편집: Adam Danz 2018년 7월 13일
Those dates work for me and I don't have any errors. In this example I set the datetime limits to your date/times above.
figure
ax1 = axes;
ax1_XLim = datetime([2018,2018],[7,7],[11,11],[15,16],[52,01],[0,0]);
xl = datenum(ax1_XLim);
xlim(xl)
set(ax1, 'xtick', xl)
datetick(ax1, 'x', 'mmm dd, yyyy, HH:MM', 'keepticks')

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

추가 답변 (1개)

Vasileios Leivadas
Vasileios Leivadas 2018년 7월 13일

0 개 추천

Cool! I fixed it but the problem was actually at the XTick rather than the limit. Now I am getting some weird figure though, where basically my plots are being erased as a result of the new axes. It seems like the whole Cartesian box is duplicated and put on top of the previous one. The related code is:
x = time_dat;
y1 = true_accel_x;
y2 = true_accel_y;
y3 = true_accel_z;
y7 = T.Accel_Mag;
plot(x,y1,'b-o',x,y2,'g-o',x,y3,'r-o')
hold on
plot(x,y7,'m-o','LineWidth',0.5,'MarkerEdgeColor','c');
legend('Acc_X','Acc_Y','Acc_Z', 'AccelMag');
hold off
xlabel('Time (s)');
ylabel('Acceleration (G)');
ax1 = gca;
% ax1.Box = 'off';
ax1.Box = 'off';
ax1_Pos = ax1.Position;
ax1_XLim = ax1.XLim;
x1 = datenum(ax1_XLim);
ax1_YLim = ax1.YLim;
ax1_start = ax1.XLim(1);
%ax1_end = ax1.XLim(2);
Ticks = [ax1_start time_dat(51-1) time_dat(86-1) time_dat(129-1) time_dat(170-1)];
x2 = datenum(Ticks);
ax2_labels = {'z acc test','setup1','y acc test','setup2','x acc test'};
ax2 = axes('Position', ax1_Pos, 'XLimMode', 'manual', 'XLim', x1, 'YLim', ax1_YLim, 'XAxisLocation', 'top', 'YAxisLocation', 'right','XTickMode', 'manual');
ax2.XTick = x2;
ax2.XTickLabel = ax2_labels;

댓글 수: 4

Adam Danz
Adam Danz 2018년 7월 13일
The comment in my original answer suggested it was the xticks but in addition to that, your axis limits were also incorrect since you were using datetime values to set the limits.
Regarding your new problem, why do you need to create a second axis over top of the first one?
Vasileios Leivadas
Vasileios Leivadas 2018년 7월 14일
I don't want to create the new axis 'on top' rather than 'to the top' and right respectively for x and y just to add a new range of labelling to my graph.
Adam Danz
Adam Danz 2018년 7월 15일
편집: Adam Danz 2018년 7월 16일
I see. You need to add this to your axes()...
ax2 = axes( ..., 'Color', 'None', ... );
That will make the 2nd axis transparent so you can see axis 1 as well.
Vasileios Leivadas
Vasileios Leivadas 2018년 7월 16일
Thanks buddy! You've been very helpful!

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

카테고리

도움말 센터File Exchange에서 Axes Appearance에 대해 자세히 알아보기

제품

릴리스

R2018a

태그

질문:

2018년 7월 12일

편집:

2018년 7월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by