Azzera filtri
Azzera filtri

Including a value inside an annotation

8 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Walter Roberson
Walter Roberson il 28 Ott 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 Commento
Paul Barrette
Paul Barrette il 29 Ott 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!

Accedi per commentare.

Più risposte (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 28 Ott 2023
Modificato: Sulaymon Eshkabilov il 28 Ott 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 Commenti
Paul Barrette
Paul Barrette il 29 Ott 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.

Accedi per commentare.

Categorie

Scopri di più su Historical Contests in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by