Defining a Y-Based custom datatip with a non-auto format

3 ビュー (過去 30 日間)
Yoad Pilosof
Yoad Pilosof 2024 年 5 月 2 日
回答済み: Milan Bansal 2024 年 5 月 2 日
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

採用された回答

Milan Bansal
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 件)

カテゴリ

Help Center および File ExchangeInstall Products についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by