How to plot label in multiple lines in matrix form?

2 ビュー (過去 30 日間)
Aureja
Aureja 2017 年 11 月 19 日
コメント済み: Aureja 2017 年 11 月 19 日
I am testing different conditions and I want to plot conditions I am comparing in a matrix form like shown in the figure. Any ideas how I can do that? Any help is greatly appreciated.
So far I've only managed to put it in two lines, like in the second picture, using code:
% example:
x = 1:1:10;
y = rand(10,1);
txt = {'1 2', '1 3', '1 4', '1 5', '2 3', '2 4', '2 5', '3 4', '3 5','4 5'};
labels = cellfun(@(x) strrep(x,' ','\newline'),txt,'UniformOutput',false);
figure()
plot(x,y,'.','MarkerSize',50)
xticks(x)
xticklabels(labels)
xlim([0.5 10.5])

採用された回答

Rik
Rik 2017 年 11 月 19 日
You only provide one space. How can Matlab know you sometimes mean multiple spaces?
Because of the structure your labels seem to have, I would suggest using
mini_fun=@(a,b) ...
[repmat(' ',1,a-1) ...
num2str(a) ...
repmat(' ',1,b-a-1) ...
num2str(b) ...
repmat(' ',1,5-b)];
index1=[1 1 1 1 2 2 2 3 3 4];
index2=[2 3 4 5 3 4 5 4 5 5];
%or generate these two vectors with combnk
txt=cellfun(mini_fun,num2cell(index1),num2cell(index2),'uni',false);
  1 件のコメント
Aureja
Aureja 2017 年 11 月 19 日
Thank you! This worked perfectly. I needed ' ' between all the variables so I modified mini_fun like this:
mini_fun=@(a,b) ...
[repmat(' ',1,a-1) ...
num2str(a) ...
repmat(' ',1,b-a) ...
num2str(b) ...
repmat(' ',1,5-b)];

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by