2011-12-06 79 views
-1

我有一些代碼,我一直在試圖對它做一些小的調整。它曾經使用fgets從一行中加載單個字符,並用它在3D圖中對點進行着色。所以它會讀取瞭解Matlab代碼

a 
p 
p 
n 
c 

然後使用其他數據文件來分配什麼x,y,z點給予這些。結果是一個真的很漂亮三維圖。

我已經編輯輸入文件,所以它讀取

0 
1 
1 
0 
2 
2 
0 

,我希望它的顏色數相同的顏色。

這是我的代碼至今得到:

function PlotCluster(mcStep) 

    clear all 

    filename = input('Please enter filename: ', 's'); 

    disp('Loading hopping site coordinates ...') 
    load x.dat 
    load y.dat 
    load z.dat 
    temp = z; 
    z = x; 
    x = temp; 
    n_sites = length(x); 

     disp('Loading hopping site types ...') 
    fp = fopen([filename]); 
    data = load(filename); %# Load the data 

    % Plot the devices 
    % ---------------- 

    disp('Plotting the sample surface ...') 
    figure 
    disp('Hello world!') 

    ia = data == 0; 
    in = data == 1; 
    ip = data == 2; 
    disp('Hello Again') 

    plot3(x(ia),y(ia),z(ia),'b.') %,'MarkerSize',4) 
    hold on 
    plot3(x(ic),y(ic),z(ic),'b.') %,'MarkerSize',4) 
    plot3(x(in),y(in),z(in),'g.') %,'MarkerSize',4) 
    plot3(x(ip),y(ip),z(ip),'r.') %,'MarkerSize',4) 
    daspect([1 1 1]) 
    set(gca,'Projection','Perspective') 
    set(gca,'FontSize',16) 
    axis tight 
    xlabel('z (nm)','FontSize',18) 
    ylabel('y (nm)','FontSize',18) 
    zlabel('x (nm)','FontSize',18) 
    %title(['Metropolis Monte Carlo step ' num2str(mcStep)]) 

    view([126.5 23]) 

我的問題是,我得到這個錯誤

Index exceeds matrix dimensions. 

Error in PlotCluster (line 34) 
    plot3(x(ia),y(ia),z(ia),'b.') %,'MarkerSize',4) 

而且我不明白爲什麼IA會去超出x數組的範圍。是否將fgets更改爲load陳述?這是唯一的方法讓它讀取正確的數字(不是49s和50s,這是非常奇怪的)。

堅持我的主要位是這些行(其中數字用來對應'a', 'N', 'p' 等)

ia = data == 0; 
    in = data == 1; 
    ip = data == 2; 

它們看起來像暗示if語句與分配從dataia等其中ia成爲一個數組。但我不確定。

任何幫助理解這將不勝感激。

+0

我已經解決了這個問題,我沒有正確更新我的輸入。 爲了解決這個問題:'ia = data == 0'意味着'使數組的大小與數據相同,並填充1或0,具體取決於邏輯(數據== 0 )是真或假' – Pureferret

+1

如果它解決了您的問題,請將其作爲答案發布。 – Mat

+0

對不起,在這個新的。會這樣做! 剛剛被告知_低於100的聲譽的用戶在詢問後8小時內無法回答自己的問題。您可以在3小時內自行回答。在此之前請使用評論,或者編輯您的問題。 – Pureferret

回答

0

我修復了這個問題,我沒有正確更新我的輸入。爲了解決這個問題:ia = data == 0表示'創建一個與數據大小相同的數組,並根據邏輯(數據== 0)是否爲真來填充1或0,或者假'