2014-03-06 127 views
0

我是一個Java程序員,沒有matlab的背景,因此我真的無法用MATLAB的這些代碼行。當我運行的代碼,我得到了一個錯誤:解釋matlab代碼

??? Undefined function or variable 'nfile'. 

Error in ==> texture_id at 29 
fprintf(' \nneural network processing \n',nfile); 

我明白'path'是存儲字符串變量,'demo'是布爾值,但對於其他行,我不想承擔它做什麼..你能幫我解釋一下每一行嗎?

下面的代碼:

path = 'C:\Users\Dais\Documents\MATLAB\Data Sets\'; 

demo = true; 

elfile = dir('*.jpg'); 

[lu ri] = size(elfile); feat=zeros(lu,29); nomf=cell(lu,1); 
for nfi = 1:lu 
    nfile = elfile(nfi).name; 
    fprintf(' feature extraction file: %s \n',nfile); 
    nomf{nfi} = upper(nfile); 
    feat(nfi,:) = feature_ex([path nfile],demo); 
end 

fprintf(' \nneural network processing \n',nfile); 

回答

1

我想這裏發生了什麼是elfile = dir('*.jpg');在本地目錄中找不到任何jpeg,因此lu是空的,並且nfile永遠不會被填充。在代碼中放置一個斷點並檢查它。我會成立了循環的方法是這樣的:

for nfi=1:numel(elfile) 

正如@Rody Oldenhuis說,使用文檔和幫助elarn瞭解每個功能(或按F1當光標在函數名)但這應該讓你開始..

%Looks for all files with extention .jpg in current directory 
elfile = dir('*.jpg'); 

%lu and ri hold the rows, column lengths of elfile respectively 
[lu ri] = size(elfile); 

%creates an array of zeros of dimensions lu rows by 29 columns 
feat=zeros(lu,29); 

%creates an empty cell array (doc cell) dimensions lu rows by 1 
nomf=cell(lu,1); columns 
for nfi = 1:lu           %look through all files 
    nfile = elfile(nfi).name;       %get index nfi file 
    fprintf(' feature extraction file: %s \n',nfile); %print string 
    nomf{nfi} = upper(nfile);       %upper case 
    feat(nfi,:) = feature_ex([path nfile],demo);  %some external function 
end 

fprintf(' \nneural network processing \n',nfile);  %print string 
1

不是說明一切,一切關於MATLAB,我會這樣說:MATLAB是互動!而且,爲了支付MATLAB的好處之一就是該文檔非常棒,獲得幫助非常簡單。

例如,您可以在MATLAB命令行中鍵入help <command>,並獲得該命令的簡短幫助,或doc <command>以獲取完整的文檔,通常帶有示例和演示。整個文檔也是在線的,如果你喜歡谷歌並且在瀏覽器中。

如果您的腳本或函數或類出現問題,您可以發出dbstop if error,以便在發生錯誤時將其放入調試器,然後您可以查看錯誤發生前所有變量的內容,輸入新命令來調查錯誤等。您可以通過單擊您想要中斷的位置旁邊的行號來設置斷點,然後dbstep僅需一步,dbup可以將您移動到某個級別等位置。請參閱doc dbstop

您可以選擇部分代碼並按F9鍵,這將執行這些代碼行。請注意,這相當於將代碼複製粘貼到命令窗口並運行它,所以您通常會遇到未定義變量(以及類似問題)的問題(這種情況或類似情況是我懷疑在您的特定情況下發生的情況,因爲你發佈的代碼不應該給出這個錯誤)。