how to change datatip from datenum format to datestr format
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi everyone,
basically I'm trying to change datatip properties, in particular I'm trying to change the format of datatips for dates from datanum format to a string format (as 'dd/MM/yyyy').
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/290613/image.jpeg)
In this case, the variables plotted are:
- 'dates': contains the dates as "datenum" format so its format is 'double';
- 'ptot_cumpl' and 'capInit' are 'double' format variables.
- I'm trying to understand how to do it for the "Total Portfolio" line that is the subplot1, then I'll do the same for all the others.
aa = plot(dates,ptot_cumpl+capInit,'k-','LineWidth',2);
xlim([dates(1) dates(end)+20]);
datetick ('x','mmm YYYY','keeplimits','keepticks');
grid on;
ytickformat('usd');
aa.DataTipTemplate.DataTipRows(1).Label = 'Date';
aa.DataTipTemplate.DataTipRows(2).Label = 'Cum Profit';
aa.DataTipTemplate.DataTipRows(2).Format = '%.0f';
Then I tried to change the values for "DataTipRows(1)" but I got this error:
aa1 = dataTipTextRow('Date',datestr(dates,'dd/MM/yyyy'));
aa.DataTipTemplate.DataTipRows(1) = aa1;
Error using matlab.graphics.datatip.DataTipTemplate/set.DataTipRows
Value must be a string scalar or a character vector with the name of a data property, such as 'XData', or it must be a vector or a function handle.
I tried also writing the last 2 lines as follows but it seems all the contained data is applied to each x axis step:
aa.DataTipTemplate.DataTipRows(1) = datestr(dates,'dd/MM/yyyy');
Warning: Error creating or updating Text
Error in value of property String
Text length might be too long to display.
Warning: Error creating or updating Text
Error in value of property String
Text length might be too long to display.
Warning: Error creating or updating Text
Error in value of property String
Text length might be too long to display.
Warning: Error creating or updating Text
Error in value of property String
Text length might be too long to display.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/290614/image.jpeg)
I tried then also converting the date variable to a string array but I get again an error.
aa.DataTipTemplate.DataTipRows(1) = string(datestr(dates));
Error using matlab.graphics.datatip.DataTipTemplate/set.DataTipRows
Label must be either a string scalar or character vector.
Thanks everyone! ;)
0 commentaires
Réponse acceptée
Cris LaPierre
le 5 Mai 2020
Modifié(e) : Cris LaPierre
le 5 Mai 2020
I would suggest converting your dates to datetimes. Then you can just set the formatting of your displayed dates to be whatever you want.
dates = datetime(2012,01,01,0,0,0):days(1):datetime(2012,12,31,24,0,0);
temp = rand(size(dates));
aa = plot(dates,cumsum(temp));
aa.DataTipTemplate.DataTipRows(1).Label = 'Date';
aa.DataTipTemplate.DataTipRows(1).Format = "dd/MM/yyyy";
aa.DataTipTemplate.DataTipRows(2).Label = 'Cum Profit';
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Title dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!