Defining a Y-Based custom datatip with a non-auto format
3 次查看(过去 30 天)
显示 更早的评论
I am trying to create a custom datatip, where the value is a 2-input function, and that the format is not 'auto'. The following code shows the main part of the problem:
p = plot((1:10).^2); % Define a line
% X-Based Datatip, with `auto` formatting
p.DataTipTemplate.DataTipRows = [p.DataTipTemplate.DataTipRows; dataTipTextRow("A", @(x) 2*x, 'auto')];
% X-Based Datatip, with floating point formatting
p.DataTipTemplate.DataTipRows = [p.DataTipTemplate.DataTipRows; dataTipTextRow("B", @(x) 2*x, '%.3f')];
% Y-Based Datatip, with `auto` formatting
p.DataTipTemplate.DataTipRows = [p.DataTipTemplate.DataTipRows; dataTipTextRow("C", @(~,y) 2*y, 'auto')];
% Y-Based Datatip, with floating point formatting
p.DataTipTemplate.DataTipRows = [p.DataTipTemplate.DataTipRows; dataTipTextRow("D", @(~,y) 2*y, '%.3f')];
I get an error on the last line:
Error using matlab.graphics.datatip.DataTipTemplate/set.DataTipRows
Not enough input arguments.
I am not sure if I am using this feature incorrectly, or if this is a bug
0 个评论
采纳的回答
Milan Bansal
2024-5-2
Hi Yoad Pilosof,
I see that you are trying to add a Y-based dataTipTextRow to your plot but are facing an error when using a 2-input lambda function and setting the custom format to display 3 digits after the decimal.
A workaround to resolve this error is to use the sprintf function within the lambda function provided to dataTipTextRow. The sprintf function allows for precise control over the format of the output string. Please change the last line in your code as shown in the code snippet below:
p = plot((1:10).^2); % Define a line
% X-Based Datatip, with `auto` formatting
p.DataTipTemplate.DataTipRows = [p.DataTipTemplate.DataTipRows; dataTipTextRow("A", @(x) 2*x, 'auto')];
% X-Based Datatip, with floating point formatting
p.DataTipTemplate.DataTipRows = [p.DataTipTemplate.DataTipRows; dataTipTextRow("B", @(x) 2*x, '%.3f')];
% Y-Based Datatip, with `auto` formatting
p.DataTipTemplate.DataTipRows = [p.DataTipTemplate.DataTipRows; dataTipTextRow("C", @(~, y) 2*y, 'auto')];
% Y-Based Datatip, with floating point formatting
%% Using sprintf in the lambda function.
p.DataTipTemplate.DataTipRows = [p.DataTipTemplate.DataTipRows; dataTipTextRow("D", @(~,y) sprintf('%.3f', 2*y))];
% Verify the result
datatip(p,8,64);
Please refer to the documentation link to learn more about sprintf function.
Hope this helps!
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Install Products 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!