Evgeny
Evgeny

Reputation: 47

align text with tab and insert in figure

I want to add a description of a model in a separate subplot figure in Matlab. The description is formed by reading user input data which can have different length and I want it to be displayed aligned in columns, something like:

player 1____Andrew______blue

player 2____Bob_________green

player 3____Johnathan___red

(with spaces instead of '_')

I can't figure out how to do so. I tried tabbing with '\t', 'char(9)'; specifying explicitly number of spaces; and trying to create a cell array - nothing worked. Any ideas why and are there other ways to do so? Many thanks in advance for any comments/suggestions and thanks for your time!

clf 
str1=[]; str2=[]; str3=[]; str4=[];
names={'Andrew', 'Bob', 'Johnathan'};
colorsorder={'blue', 'green', 'red'};

figure(1)
d1=subplot(2,2,1);
for i=1:3
    newstr=['player ', num2str(i), '\t', char(names(i)), '\t', ...
            'color= ', char(colorsorder(i)), ' \n'];
    str1=strcat(str1, newstr);
end
y=ylim(d1);
t=text(0, y(2), sprintf(str1), 'Parent', d1);
set(t, 'FontSize', 15)
axis off

d2=subplot(2,2,2);
for i=1:3
    newstr=['player ', num2str(i), char(9) , char(names(i)), char(9), ...
            'color= ', char(colorsorder(i)), ' \n'];
    str2=strcat(str2, newstr);
end
y=ylim(d2);
t=text(0, y(2), sprintf(str2), 'Parent', d2);
set(t, 'FontSize', 15)
axis off

d3=subplot(2,2,3);
tab=15;
for i=1:3
    newstr=['player ', num2str(i), blanks(5), char(names(i)),...
             blanks(tab-length(char(names(i)))),...
            'color= ', char(colorsorder(i)), ' \n'];
    str3=strcat(str3, newstr);
end
y=ylim(d3);
t=text(0, y(2), sprintf(str3), 'Parent', d3);
set(t, 'FontSize', 15)
axis off

d4=subplot(2,2,4);
dtable=cell(3, 3);
for i=1:3
    dtable(i, 1)={['player ', num2str(i)]};
    dtable(i, 2)={char(names(i))};
    dtable(i, 3)={['color ',char(colorsorder(i))]};
    str4=strcat(str4, strjoin(dtable(i,:), '\t'), '\n');
end
y=ylim(d4);
t=text(0, y(2), sprintf(str4), 'Parent', d4);
set(t, 'FontSize', 15)
axis off

Upvotes: 1

Views: 1034

Answers (1)

Lati
Lati

Reputation: 341

Matlab tex interpreter has problem with tab character (\t). I would go for latex interpreter and also use table for proper alignment. Look at the following example:

clf 
str1=[]; str2=[]; str3=[]; str4=[];
names={'Andrew', 'Bob', 'Johnathan'};
colorsorder={'blue', 'green', 'red'};

figM = figure(1);
d1=subplot(2,2,1);

latexString = '$$\begin{tabular}{lll}';
for i=1:3
    latexString = [latexString sprintf('player %s & %s & color= %s\\\\ ', num2str(i), names{i}, colorsorder{i})];  
end
latexString = [latexString '\end{tabular}$$'];

axis off

text('String',latexString,...
'Interpreter','latex',... 
'Position',[.5 .5],...
'FontSize',16)

Upvotes: 1

Related Questions