Effacer les filtres
Effacer les filtres

Including a value inside an annotation

8 vues (au cours des 30 derniers jours)
Paul Barrette
Paul Barrette le 28 Oct 2023
Commenté : Paul Barrette le 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!

Réponse acceptée

Walter Roberson
Walter Roberson le 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.
  1 commentaire
Paul Barrette
Paul Barrette le 29 Oct 2023
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!

Connectez-vous pour commenter.

Plus de réponses (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 28 Oct 2023
Modifié(e) : Sulaymon Eshkabilov le 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 commentaires
Sulaymon Eshkabilov
Sulaymon Eshkabilov le 28 Oct 2023
Most welcome -sir!
Paul Barrette
Paul Barrette le 29 Oct 2023
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Historical Contests dans Help Center et File Exchange

Tags

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by