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

3 views (last 30 days)
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 Comment
Sim
Sim on 21 Feb 2022
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".

Sign in to comment.

Accepted Answer

Sim
Sim on 21 Feb 2022
Edited: Sim on 21 Feb 2022
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!