Including a value inside an annotation
14 views (last 30 days)
Show older comments
Paul Barrette
on 28 Oct 2023
Commented: Paul Barrette
on 29 Oct 2023
I am trying to include a number which changes inside a recurring annotation.
This works - the number 48 is hard-coded:
annotation('textbox',[0.42 0.864 0.1 0.1],'String','L. St. Lawrence - INCREASE IN ISI (n=48)','EdgeColor','none', 'FontSize',12,'Color','black','FontWeight','bold')
This does not (note the insertion of a number q, which has been pre-allocated a value of 48):
annotation(['textbox',[0.42 0.864 0.1 0.1],'String','L. St. Lawrence - INCREASE IN ISI (n=',num2str(q),')','EdgeColor','none','FontSize',12,'Color','black','FontWeight','bold'])
The error I get is:
Error using annotation
First argument must be a valid annotation type or a handle to a figure, uipanel, or uitab.
Thanks for helping!
0 Comments
Accepted Answer
Walter Roberson
on 28 Oct 2023
You have
annotation(['textbox',[0.42 0.864 0.1 0.1],'String','L. St. Lawrence - INCREASE IN ISI (n=',num2str(q),')','EdgeColor','none','FontSize',12,'Color','black','FontWeight','bold'])
This includes a [] expression that must be evaluated first, and the result of the [] expression will be passed as a parameter to annotation()
The [] expression is
['textbox',[0.42 0.864 0.1 0.1],'String','L. St. Lawrence - INCREASE IN ISI (n=',num2str(q),')','EdgeColor','none','FontSize',12,'Color','black','FontWeight','bold']
which asks to [] together characters and numeric values.
When you [] together characters and numeric values, the numeric values are converted using char() . Note that char() of a numeric value is not the printable representation of the numeric value. If you char(65) for example you do not get '65' (the character for the digit 6 followed by the character for the digit 5). Instead, char() does a uint16() of the provided value (except it rounds down) and then treats the resulting integer as a Unicode Code Point. char(65) requests the 66'th Unicode Code Point (they start at 0) which happens to be the character 'A'
So ['textbox',[0.42 0.864 0.1 0.1]] would be the same as [['textbox',char([0.42 0.864 0.1 0.1])] which would be the same as ['textbox',char(uint16(floor([0.42 0.864 0.1 0.1])))] which would be ['textbox', char(uint16([0 0 0 0]))] which would be ['textbox', char(0), char(0), char(0), char(0)] so would be a character vector that started with 'textbox' and was then 4 binary characters.
This is clearly no what you want.
What you might have wanted might have been
annotation('textbox',[0.42 0.864 0.1 0.1],'String',['L. St. Lawrence - INCREASE IN ISI (n=',num2str(q),')'],'EdgeColor','none','FontSize',12,'Color','black','FontWeight','bold'])
which leaves 'textbox' and the double precision numbers as separate parameters, and constructs a character vector from the 'L. St. Lawrence' and so on through to the ')' and passes that as the parameter after 'String'
So your [] were out of place.
These days I would probably suggest
annotation('textbox',[0.42 0.864 0.1 0.1],'String',"L. St. Lawrence - INCREASE IN ISI (n="+q+")",'EdgeColor','none','FontSize',12,'Color','black','FontWeight','bold'])
when you use "" objects those are string() objects whereas '' objects are character vectors. string() objects define a + operation that converts numeric values to representable text and append that to the end of the string object.
More Answers (1)
Sulaymon Eshkabilov
on 28 Oct 2023
Edited: Sulaymon Eshkabilov
on 28 Oct 2023
Here is the solution to this issue:
q = 48;
DIM = [0.42 0.864 0.1 0.1];
STR = strcat('L. St. Lawrence - INCREASE IN ISI (n= ', num2str(q), ' )');
annotation('textbox',DIM,'String',STR,'FitBoxToText','on','EdgeColor','none','FontSize',12,'Color','black','FontWeight','bold');
3 Comments
See Also
Categories
Find more on NaNs in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!