how to change datatip from datenum format to datestr format

조회 수: 12 (최근 30일)
endystrike
endystrike 2020년 5월 5일
댓글: endystrike 2020년 5월 5일
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').
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.
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! ;)

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 5월 5일
편집: Cris LaPierre 2020년 5월 5일
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';
  댓글 수: 1
endystrike
endystrike 2020년 5월 5일
solved following your suggestions, thanks a lot! ;)
date_ax = datetime(dates,'ConvertFrom','datenum');
aa = plot(date_ax,ptot_cumpl+capInit,'k-','LineWidth',2);
aa.DataTipTemplate.DataTipRows(1).Label = 'Date';
aa.DataTipTemplate.DataTipRows(1).Format = "dd/MM/yyyy";
aa.DataTipTemplate.DataTipRows(2).Label = 'Cum Profit';
aa.DataTipTemplate.DataTipRows(2).Format = '%.0f';
xlim([date_ax(1) date_ax(end)+20]);
datetick ('x', 'mmm YYYY', 'keeplimits', 'keepticks');
grid on;
ytickformat('usd');

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by