Clear Filters
Clear Filters

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

1 view (last 30 days)
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

Accepted Answer

Milan Bansal
Milan Bansal on 2 May 2024
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!

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!