Defining a Y-Based custom datatip with a non-auto format
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Yoad Pilosof
le 2 Mai 2024
Réponse apportée : Milan Bansal
le 2 Mai 2024
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 commentaires
Réponse acceptée
Milan Bansal
le 2 Mai 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!
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Install Products 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!