2012-05-25 27 views
1

我無法弄清楚如何在我的圖中顯示4個變量。4D用數據光標繪製顯示變量Matlab

欲改變獨立變量X,V,以產生因變量Y和Z. Y是X和V和Z的函數爲Y的函數,並且X.

這可能更容易看到依賴關係:X,V,Y(X,V),Z(X,Y(X,V))。

我已經使用了surf函數來繪製X,Y,Z,但我也想知道V的值,目前我無法確定。

下面是一些測試數據來說明:

X = linspace(1,5,5) 
V = linspace(1,5,5) 
Capture = [] 
for j = 1:length(V) 
Y = X.*V(j) 
Capture = [Capture;Y] 
end 
[X,V] = meshgrid(X,V); 
Z = Capture.*X 
surf(X,Y,Z) 

如果我使用數據光標,我可以看到X,Y,Z值,但我也想知道五我的價值觀知道我有它設置的方式是正確的,因爲如果我做兩個地塊,說:

surf(X,Y,Z) 
surf(X,V,Z) 

,然後使用數據光標移動到同一點X和Z兩者的圖形值欲訴和Y是他們應該爲那個點(X,Z)。

是否有無需顯示X,Y,V和Z的值而無需生成兩個單獨的圖?

謝謝!

回答

3

使用顏色作爲你的第四維是一種可能性(它對你來說看起來不錯是一個品味問題)。

surf(X,Y,Z,V); #% 4th arg (V) is mapped onto the current colormap 

你可以change the colormap適合你的口味。

colorbar #% displays a colorbar legend showing the value-color mapping 

編輯:提問者想要精確地看到未顯示的數組中的數據,而不僅僅是一種顏色。這是自定義數據遊標函數的工作。下面我使用純粹的匿名函數實現了這個功能;在函數文件中執行它會稍微簡單一些。

#% Step 0: create a function to index into an array... 
#% returned by 'get' all in one step 
#% The find(ismember... bit is so it returns an empty matrix... 
#% if the index is out of bounds (if/else statements don't work... 
#% in anonymous functions) 
getel = @(x,i) x(find(ismember(1:numel(x),i))); 

#% Step 1: create a custom data cursor function that takes... 
#% the additional matrix as a parameter 
myfunc = @(obj,event_obj,data) {... 
['X: ' num2str(getel(get(event_obj,'position'),1))],... 
['Y: ' num2str(getel(get(event_obj,'position'),2))],... 
['Z: ' num2str(getel(get(event_obj,'position'),3))],... 
['V: ' num2str(getel(data,get(event_obj,'dataindex')))] }; 

#% Step 2: get a handle to the datacursormode object for the figure 
dcm_obj = datacursormode(gcf); 

#% Step 3: enable the object 
set(dcm_obj,'enable','on') 

#% Step 4: set the custom function as the updatefcn, and give it the extra... 
#% data to be displayed 
set(dcm_obj,'UpdateFcn',{myfunc,V}) 

現在提示應顯示額外的數據。請注意,如果您更改圖中的數據,則需要重複Step 4以將新數據傳遞到該函數中。

+0

感謝tmpearce的迴應,我知道這個解決方案,問題是它沒有給我準確的數據,而不是顏色欄,我寧願有那個V的確切元素。這可能嗎? – Tim

+0

啊,我明白了。更新了有關自定義數據提示的信息。 – tmpearce

+0

tmpearce,非常感謝您的回覆,這個工作很完美,我不確定我對它的理解是否太好,但它可能會最好地理解數據光標的代碼,而無需先添加任何附加代碼。再次感謝! – Tim