필터 지우기
필터 지우기

Including a value inside an annotation

조회 수: 8 (최근 30일)
Paul Barrette
Paul Barrette 2023년 10월 28일
댓글: Paul Barrette 2023년 10월 29일
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
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 10월 28일
Most welcome -sir!
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