Display cell array elements/texts in a figure, (i) by using a single index, (ii) separated by a comma, and (iii) by using annotation.

2 ビュー (過去 30 日間)
Hi, I have a cell array containing some text:
groups = {'London-Berlin','Rome-Paris','Madrid-Athens','Boston-Dallas'};
I would like to display the cell array elements / texts in a figure,
  1. by using a single index
  2. separated by a comma
  3. by using annotation
In my attempt, I am able to fullfil conditions (1) and (3), but not condition (2), i.e. display texts separated by a comma:
figure('color',[1 1 1])
i = [1 2]; % using a single index
groups = {'London-Berlin','Rome-Paris','Madrid-Athens','Boston-Dallas'};
annotation('textbox',[0.7 0 0 0.5],'String',groups(i),'FitBoxToText','on'); % annotation
Any clue?
I would like to display as:
  1 件のコメント
Sim
Sim 2022 年 2 月 21 日
Just for information, I produced the second figure with the desired layout of my text with this code
figure('color',[1 1 1])
annotation('textbox',[0.7 0 0 0.5],'String',{[groups{1},', ',groups{2}]},'FitBoxToText','on');
but obviously, it does not fullfil condition (1), i.e. to use a single index "i".

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

採用された回答

Sim
Sim 2022 年 2 月 21 日
編集済み: Sim 2022 年 2 月 21 日
I think I found a solution:
i = [1 3 4]; % single index
groups = {'London-Berlin','Rome-Paris','Madrid-Athens','Boston-Dallas'};
figure('color',[1 1 1])
str = sprintf(', %s',groups{i}); % texts separated by a comma
str = strtrim(str(2:end)); % (remove the first comma and possible empty spaces)
annotation('textbox',[0.4 0 0 0.5],'String',str,'FitBoxToText','on'); % annotation

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by