2013-08-20 34 views
0

我的變量及其值,在if條件語句Matlab的:如果條件對空矩陣工作,不能正常工作

leftoverROI1s{1}= [11 15]; 

missinglabelsinimage{1}是一個空矩陣。

我想執行一個for循環只有當我在if聲明兩個條件都爲真,即:

if ~isempty(leftoverROI1s{1}) && ~isempty(missinglabelsinimage{1}) 

    for % loop for each element in non-empty `missinglabelsinimage` structure array. 
     % Add a scalar to each element of non-empty `missinglabelsinimage` structure array 
     ... 
    end % end for loop 

end % end if 

我的程序控制進入for循環(這是我期望的,它不應該,如果有一個空missinglabelsinimage{1})和控制正在missinglabelsinimage{1}(空矩陣),這顯然給了我,我想一個標量添加到我的「非空」 missinglabelsinimage{1}

我無法理解的錯誤在我的錯誤條件。任何幫助,將不勝感激。

PS:我查了上述變量

~isempty(missinglabelsinimage{1}) 
ans =  
    0 

~isempty(leftoverROI1s{1}) 
ans = 
    1 

missinglabelsinimage{1} 
ans = 
    Empty matrix: 1-by-0 
+2

我不明白你在問什麼。如果'missinglabelsinimage {1}'是空的,並且你說這是,那麼你的'if'語句將阻止'for'循環執行,這看起來正是你想要的。 – horchler

+1

報告完整的錯誤消息和一個可重複的示例。 – Oleg

+0

編寫一個能夠重現問題的函數(不是腳本)。 –

回答

1

我懷疑有代碼一個錯字的地方,你沒有顯示。減少你的例子,它的最基本的形式(總是一個好主意,試圖找到一個bug):

a = []; 
b = [1 2 3]; 
display(~isempty(a)) 
display(~isempty(b)) 

if ~isempty(a) && ~isempty(b) 
    disp('we passed the if') 
else 
    disp('we are in the else') 
end 

結果輸出

ans = 
    0 
ans = 
    1 
we are in the else 

完全按照自己的期望。如果你得到了不同的東西,那麼你使用的代碼不是你正在顯示的代碼......在某處是否有類似的(錯誤類型)變量?嘗試做一個clear all,然後運行一個能夠重現問題的最小例子。