Variable horizontal alignment of cell array's strings in annotation

In the following example, I'm trying to center align the second string of arrcell while keeping the first and third strings aligned to the left
cases = 18;
percent = 0.3;
dim = [0.1 0.5 0.1 0.1];
arrcell = {['there are ',num2str(cases),' cases'],['- - - -'],['the percent is ',num2str(percent)]};
an = annotation('textbox',dim,'String',arrcell,'FitBoxToText','on');
set(an, {'HorizontalAlignment'}, {'left';'center';'left'});
but I get the error
Error using matlab.graphics.shape.TextBox/set
Value cell array handle dimension must match handle vector length.
What is wrong?
This is what I would like to obtain

回答 (2 件)

Ameer Hamza
Ameer Hamza 2020 年 4 月 7 日
編集済み: Ameer Hamza 2020 年 4 月 7 日

1 投票

cases = 18;
percent = 0.3;
dim = [0.1 0.5 0.1 0.1];
arrcell = {['there are ',num2str(cases),' cases'],['- - - -'],['the percent is ',num2str(percent)]};
an1 = annotation('textbox',dim,'String',arrcell(3), 'FitBoxToText','on', ...
'Margin',0, 'EdgeColor', [0 0 0], 'HorizontalAlignment', 'left', ...
'EdgeColor', 'none');
an1.Position(1:2) = dim(1:2);
drawnow;
pos1 = an1.Position;
an2 = annotation('textbox',[pos1(1) pos1(2)+pos1(4) pos1(3) pos1(4)],'String',arrcell(2), ...
'Margin',0, 'EdgeColor', [0 0 0], 'HorizontalAlignment', 'center', ...
'EdgeColor', 'none');
drawnow;
pos2 = an2.Position;
an3 = annotation('textbox',[pos2(1) pos2(2)+pos2(4) pos2(3) pos2(4)],'String',arrcell(1), ...
'Margin',0, 'EdgeColor', [0 0 0], 'HorizontalAlignment', 'left', ...
'EdgeColor', 'none');
pos3 = an3.Position;
annotation('rectangle', [pos1([1 2 3]) pos1(4)*3]+[-0.01 -0.01 0.02 0.02]);

6 件のコメント

giannit
giannit 2020 年 4 月 7 日
編集済み: giannit 2020 年 4 月 7 日
Thank you, I think pos is not defined though.
Anyway, the following code is less cumbersome
cases = 18;
percent = 0.3;
arrcell1 = {['there are ',num2str(cases),' cases']};
arrcell2 = {[''],['- - - -']};
arrcell3 = {[''],[''],['the percent is ',num2str(percent)]};
annotation('textbox',[.1 .5 .24 .15],'String',arrcell1,'HorizontalAlignment','left');
annotation('textbox',[.1 .5 .24 .15],'String',arrcell2,'HorizontalAlignment','center');
annotation('textbox',[.1 .5 .24 .15],'String',arrcell3,'HorizontalAlignment','left');
but as drawback we cannot use 'FitBoxToText','on' with this method.
Ameer Hamza
Ameer Hamza 2020 年 4 月 7 日
I made a mistake in pasting the code. Please check the updated code. I think your code does not produce the formatting you mentioned in the question.
giannit
giannit 2020 年 4 月 7 日
編集済み: giannit 2020 年 4 月 7 日
Thank you very much, I really appreciate your help, the only bad thing is that the margin is too small (text it touching the border).
I updated my comment, the code now produce exactly the formatting mentioned in the question.
question:
code in comment:
your code:
Ameer Hamza
Ameer Hamza 2020 年 4 月 7 日
Can you tell, which version of MATLAB are you using? When I run this code
cases = 18;
percent = 0.3;
arrcell1 = {['there are ',num2str(cases),' cases']};
arrcell2 = {[''],['- - - -']};
arrcell3 = {[''],[''],['the percent is ',num2str(percent)]};
annotation('textbox',[.1 .5 .24 .15],'String',arrcell1,'HorizontalAlignment','left');
annotation('textbox',[.1 .5 .24 .15],'String',arrcell2,'HorizontalAlignment','center');
annotation('textbox',[.1 .5 .24 .15],'String',arrcell3,'HorizontalAlignment','left');
I get this output
giannit
giannit 2020 年 4 月 29 日
Sorry for late, here is my version, is it bad?
Ameer Hamza
Ameer Hamza 2020 年 4 月 29 日
If it works for on your platform, then you should continue using this code. I tried on macOS using R2018b and R2020a, and in both cases, this code created extra space inside the box.

サインインしてコメントする。

KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 4 月 7 日
編集済み: KALYAN ACHARJYA 2020 年 4 月 7 日

0 投票

cases = 18;
percent = 0.3;
dim = [0.1 0.5 0.1 0.1];
arrcell = {['there are ',num2str(cases),' cases'],['- - - -'],['the percent is ',num2str(percent)]};
an = annotation('textbox',dim,'String',arrcell,'FitBoxToText','on');
set(an, {'HorizontalAlignment'}, {'center'});

2 件のコメント

giannit
giannit 2020 年 4 月 7 日
Thank you! Anyway, this is not exactly what I would like to obtain. Instead I would like
  • first string left aligned
  • second string center aligned
  • third string left aligned
This is why I tried with {'left';'center';'left'}` but, it doesn't work.
Any ideas?
Ameer Hamza
Ameer Hamza 2020 年 4 月 7 日
Unfortunately, such a thing is not supported in MATLAB. The only workaround is to draw each component separately. Check the code in my answer. It is cumbersome, to say the least.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

質問済み:

2020 年 4 月 7 日

コメント済み:

2020 年 4 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by