2010-03-14 200 views
0

我目前是一個begineer,我正在使用matlab來做一個數據分析。我有一個數據在第一行的文本文件格式如下: 時間;波高1;波高2; ....... 我有列,直到波高19和行總共4000行。在matlab中繪製三維圖?

第一列的數據是以秒爲單位的時間。從第2欄開始,以米爲單位的波高提升。此刻,我想要matlab在x軸上繪製一個三維圖形,y軸上的波高和波形高度(對應于波形高度編號從1到19),即列2行10中的數據有一個讓說8米這是對應於第1列第10行

波身高1和時間,我嘗試以下方法:

clear;  
filename='abc.daf';  
path='C:\D'; 

a=dlmread([path '\' filename],' ', 2, 1); 

[nrows,ncols]=size(a); 

t=a(1:nrows,1);%define t from text file 

for i=(1:20),  
    j=(2:21);  
end 

wi=a(:,j); 

for k=(2:4000),  
    l=k;  
end 

r=a(l,:); 

但是每當我使用嘗試繪製出來,for循環的Wi作品很好,但是對於r = a(l,:),這個繪圖只會給我上一次數據,但我想要繪製文件中的所有數據。

有沒有辦法可以做到這一點。我很抱歉,因爲這有點令人困惑,但如果有人能幫助我,我將非常感激。

謝謝!!!!!!!!!!

回答

0

一旦你爲你做加載你的數據在你的代碼上面的變量a應該是4000 * 20陣列。然後,您可以通過幾種不同的方式創建一個3-D圖。你可以使用功能PLOT3創建3 d線圖,繪製一條線的波高數據中的每一列:

t = a(:,1); %# Your time vector 
for i = 2:20 %# Loop over remaining columns 
    plot3(t,(i-1).*ones(4000,1),a(:,i)); %# Plot one column 
    hold on; %# Continue plotting to the same axes 
end 
xlabel('Time');   %# Time on the x-axis 
ylabel('Wave number');  %# Wave number (1-19) on y-axis 
zlabel('Wave elevation'); %# Elevation on z-axis 

另一種方法來繪製在3 d您的數據是使網狀或表面圖,分別使用功能MESHSURF。這裏有一個例子:

h = surf(a(:,1),1:19,a(:,2:20)'); %'# Plot a colored surface 
set(h,'EdgeColor','none'); %# Turn off edge coloring (easier to see surface) 
xlabel('Time');    %# Time on the x-axis 
ylabel('Wave number');  %# Wave number (1-19) on y-axis 
zlabel('Wave elevation'); %# Elevation on z-axis 
0

我不太明白你的功能在做什麼,例如,我沒有看到任何繪圖命令。

以下是我想嘗試根據您的規格進行3D繪圖:

%# Create some data - time from 0 to 2pi, ten sets of data with frequency 1 through 10. 
%# You would just load A instead (I use uppercase just so I know that A is a 2D array, 
%# rather than a vector) 
x = linspace(0,2*pi,100)';%#' linspace makes equally spaced points 
w = 1:10; 
[xx,ww]=ndgrid(x,w); %# prepare data for easy calculation of matrix A 
y = ww.*sin(xx.*ww); 
A = [x,y]; %# A is [time,data] 

%# find size of A 
[nRows,nCols] = size(A); 

%# create a figure, loop through the columns 2:end of A to plot 
colors = hsv(10); 
figure, 
hold on, 

for i=1:nCols-1, 

%# plot time vs waveIdx vs wave height 
plot3(A(:,1),i*ones(nRows,1),A(:,1+i),'Color',colors(i,:)), 

end 

%# set a reasonable 3D view 
view(45,60) 

%# for clarity, label axes 
xlabel('time') 
ylabel('wave index') 
zlabel('wave height') 
0

或者,你可以試試gnuplot。快速,免費且相對容易使用。我用它來生成數百萬行數據集的熱圖。