2017-11-17 62 views
0

的價值我有這樣一個循環:要改變一個空的變量

[centers, radii]=imfindcircles() %find circles in the image 
if isnan(centers)==1 %if centres is empty 
    centers='NA'; % assign centres a NA 
else 
    centers=centers; 
end 

我想要做的是:

  • 時沒有發現圓和centers=[],我要分配中心一個值(可能是'NA'0)。
  • 如果找到一些圓圈,則centres保持相同的數組。

該循環的目標是將centres的某個值賦值,以便即使沒有找到圓,我也可以將其保存到.xls文件中。

但我的問題是在這個循環後,即使centers=[],centres不會改變它的值。這意味着這個循環根本不起作用。那麼有人可以檢查我哪裏錯了嗎?

回答

-1
[centers,radii] = imfindcircles() %find circles in the image 

if (isempty(centers)) %if centres is empty 
    centers(1) = NaN; % assign centres a NA 
end 

更好的理論方法是,恕我直言:

if (isempty(centers)) 
    % write a dummy XLS file 
else 
    % write your values to XLS file 
end 
+3

你編輯你的答案,並完全使它與其他答案相同。從聲譽的變化來看,顯然你只是因爲有人低估了你的意見而低估了其他答案。 –

+0

是的,因爲完成單線操作的方法完全正確。 –

1

你可以這樣做:

function [ x ] = empty2nan(x, Size) 
    if isempty(x), x=nan(1,Size); end 
end 

而且

centers=empty2nan(centers,2)