Including a value inside an annotation

8 次查看(过去 30 天)
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!

采纳的回答

Walter Roberson
Walter Roberson 2023-10-28
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.
  1 个评论
Paul Barrette
Paul Barrette 2023-10-29
Thanks a lot, @Walter Roberson. Both solutions worked, notwithstanding an extra ']' at the end that I had to remove - the corrected versions are:
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')
and
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')
Tx also for the explanations - after a careful read, I understand where the problem was and why the solution offered by @Sulaymon Eshkabilov worked. Your input has turned into a mini-lecture - much appreciated!

请先登录,再进行评论。

更多回答(1 个)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023-10-28
编辑:Sulaymon Eshkabilov 2023-10-28
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 个评论
Paul Barrette
Paul Barrette 2023-10-29
Hi @Sulaymon Eshkabilov, I hope you will not mind too much my switching to @Walter Roberson's solutions. They are closer to what my code needs, plus the extra value provided.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Historical Contests 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by