2016-07-15 53 views
1

我需要繪製與在Matlab以下格式的單元陣列:繪製單元陣列

{[vector1], [vector2], ...} 

成2D圖形用所述載體作爲y的索引和矢量爲x

([vector1], 1), ([vector2], 2), ... 
+2

你真的可以給我們一些真實的數據,以便我們看到你在說什麼嗎?你的解釋不是很清楚。 – Suever

+0

因此,每個_y_值都有一個_x_值的關聯_vector_?那個怎麼樣? –

回答

2

這裏有一個簡單的選擇:

% some arbitrary data: 
CellData = {rand(10,1)*50,rand(10,1)*50,rand(10,1)*50}; 

% Define x and y: 
x = cell2mat(CellData); 
y = ones(size(x,1),1)*(1:size(x,2)); 

% plot: 
plot(x,y,'o') 
ylim([0 size(x,2)+1]) 

所以您繪製的x每個向量在一個單獨的y值:

A cell plot

它將工作只要你的單元陣列只是一個向量列表。

編輯:對於非平等的載體

你必須使用一個for循環與hold

% some arbitrary data: 
CellData = {rand(5,1)*50,rand(6,1)*50,rand(7,1)*50,rand(8,1)*50,rand(9,1)*50}; 

figure; 
hold on 
for ii = 1:length(CellData) 
    x = CellData{ii}; 
    y = ones(size(x,1),1)*ii; 
    plot(x,y,'o') 
end 
ylim([0 ii+1]) 
hold off 

Cell plot 2

希望這回答你的問題;)

+0

我的向量是不同的大小,所以cell2mat(Celldata)不起作用 – user3743825

+0

@ user3743825看到我編輯的答案 – EBH

0

沒有任何數據,這是我能想出的最好的,你想要什麼:

yourCell = {[0,0,0],[1,1,1],[2,2,2]}; % 1x3 cell 
figure; 
plot(cell2mat(yourCell)); 
ylabel('Vector Values'); 
xlabel('Index of Vector'); 

它使這樣一個情節:

enter image description here

希望這有助於。

+0

爲什麼你換了'x'和'y'軸?他希望矢量索引是一個'y'軸。 – EBH

+0

@EBH我想我一定把這個問題和另一個問題搞混了,哎呀。我改變了我的答案。抱歉。 – user3716193

1

這是我的(蠻力)解釋你的請求。有可能更優雅的解決方案。

此代碼會生成一個點圖,將來自y軸—上每個索引處的向量的值放在最下面。它可以適應不同長度的載體。您可以將其作爲矢量分佈的點圖,但如果可能出現多個相同或幾乎相同的值,則可能需要向x值添加一些抖動。

% random data--three vectors from range 1:10 of different lengths 
for i = 1:3 
    dataVals{i} = randi(10,randi(10,1),1); 
end 

dotSize = 14; 
% plot the first vector with dots and increase the dot size 
% I happen to like filled circles for this, and this is how I do it. 
h = plot(dataVals{1}, ones(length(dataVals{1}), 1),'.r'); 
set(h,'markers', dotSize); 

ax = gca; 
axis([0 11 0 4]); % set axis limits 
% set the Y axis labels to whole numbers 
ax.YTickLabel = {'','','1','','2','','3','','',}'; 

hold on; 
% plot the rest of the vectors 
for i=2:length(dataVals) 
    h = plot(dataVals{i}, ones(length(dataVals{i}),1)*i,'.r'); 
    set(h, 'markers', dotSize); 
end 
hold off 

enter image description here