Creating a mesh plot with data in a timetable
Show older comments
Hi there,
Assume that I create a time table based on a matrix as follows.
X = rand(5,3);
Time = seconds(1:5);
TT = array2timetable(X,'RowTimes',Time)
Now I want to get a meshz plot with x axis to be the Time vector and and showing dateticks as xtick labels.
I tried
meshz(TT{:,:})
but this will show xticks NOT as dateticks as in Time ?
Can anyone please help me with this ?
Thank you.
Accepted Answer
More Answers (1)
That form for meshz doesn't use any x-,y-coordinates at all -- it just plots against the ordinal position numbers of the Z arrays -- documentation says from 1:N and 1:M but observation shows the above uses 0:N-1 and 0:M-1 instead.
But, even using the meshz(X,Y,Z) syntax that would be required to put an explicit axis on the plot doesn't solve the problem as meshz isn't enhanced to accept a time or duration class input -- when you try,
>> Y=1:3; % a Y coordinate to go along with Time
>> Z=rand(numel(Y),numel(Time)); % a 2D array that matches X, Y
>> meshz(Time,Y,Z) % and goes boom!
Error using meshz (line 56)
Input arguments must be numeric or objects which can be converted to double.
>>
For something simple that you've shown here, the fix would be to just label the axis...
meshz(seconds(Time),Y,Z) % plot a meshz of the Time vector values
hAx.XAxis.TickLabels=compose('%d sec',Time); % label the x-axis as time/duration
xlabel('T'); ylabel('Y'); zlabel('Z') % label the axes
produces

4 Comments
Gayan Lankeshwara
on 18 Jun 2020
dpb
on 18 Jun 2020
t1 = datetime(2013,11,1,8,0,0);
t2 = datetime(2013,11,2,8,0,0);
Time = t1:hours(24):t2
Time.Format='yyMMM HH:mm';
>> string(Time(1:3:end))
ans =
1×9 string array
"13Nov 08:00" "13Nov 11:00" "13Nov 14:00" "13Nov 17:00" "13Nov 20:00" "13Nov 23:00" "13Nov 02:00" "13Nov 05:00" "13Nov 08:00"
>>
string honors the date format for the input datetime object.
An alternative that MIGHT work and this might be a reason to revert to it would be to convert your time axis into the venerable datenum instead of a datetime object. datenum is just a double so meshz shouldn\t care...you then have to use the (somewhat klunky) dateticks function on the axis to display the time strings.
If you can't find another way, that might in the end be the simpler than fixing up the tick labels manually.
Gayan Lankeshwara
on 21 Jun 2020
dpb
on 21 Jun 2020
I showed the datenum/datetick route above...it will work. Show code/problem/error.
NB: It is datetick singular, not dateticks
Categories
Find more on Polar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!