2015-04-07 18 views
0

我正在編寫一個腳本,用於運行圖片或樂高並輸出其大小,形狀和顏色。我們編寫腳本的方式,每當我們使用不同樂高的新圖片時,我們都必須清除變量的工作空間。我們必須對我們通過腳本運行的每個樂高進行分類。我們將變量發送到.mat文件,但每次運行腳本時似乎都不能一次保存多個變量,因此保存的變量將被替換爲新值。我已經決定最好的方法是通過循環運行.mat來檢查現有的值,如果有,則將值保存在現有的行下。任何幫助將非常感謝,因爲我是MATLAB新手,尤其是循環。幾天前我問了一個關於這個問題,但決定由於幾個障礙以新的方式處理這個問題。非常感謝。MATLAB:用於檢查現有變量的循環

編輯:

if length > 40 & length < 70 
    y_length = 'Two' 
    area_length = 2 
elseif length > 70 & length < 90 
    y_length = 'Three' 
    area_length = 3 
elseif length > 70 & length < 145 
     y_length = 'Four' 
     area_length = 4 
elseif length > 150 & length < 200 
    y_length = 'Six' 
    area_length = 6 
elseif length < 40 
    y_length = 'One' 
    area_length = 1 
elseif length > 200 
    y_length = 'Eight' 
    area_length = 8 
end 

    if strcmp(x_length,y_length) 
    shape = 'Square' 
else 
    shape = 'Rectangle' 
end 

    size = area_width * area_length ; 

    %%%%%% make sure smaller dimension always first %%%%% 
% width = smaller length 
% length = longer length 

    Cell = {Color, size, shape, x_length, y_length}; 

    % for iterations 1:block_count 
%  if Final = {Color, size, shape, x_length, y_length} 
disp(Cell)  
SaveData = {sprintf('%s, %d, %s, %s, %s', Color, size, shape, x_length, y_length)}; 
% load('Data.mat'); 
% 

    data = [data; SaveData]; 
save('Data.mat', 'SaveData'); 
if length > 40 & length < 70 
     y_length = 'Two' 
     area_length = 2 
    elseif length > 70 & length < 90 
     y_length = 'Three' 
     area_length = 3 
    elseif length > 70 & length < 145 
      y_length = 'Four' 
      area_length = 4 
    elseif length > 150 & length < 200 
     y_length = 'Six' 
     area_length = 6 
    elseif length < 40 
     y_length = 'One' 
     area_length = 1 
    elseif length > 200 
     y_length = 'Eight' 
     area_length = 8 
    end 

    if strcmp(x_length,y_length) 
    shape = 'Square' 
else 
    shape = 'Rectangle' 
end 

    size = area_width * area_length ; 

    %%%%%% make sure smaller dimension always first %%%%% 
% width = smaller length 
% length = longer length 

    Cell = {Color, size, shape, x_length, y_length}; 

    % for iterations 1:block_count 
%  if Final = {Color, size, shape, x_length, y_length} 
disp(Cell)  
SaveData = {sprintf('%s, %d, %s, %s, %s', Color, size, shape, x_length, y_length)}; 
% load('Data.mat'); 
% 

    data = [data; SaveData]; 
save('Data.mat', 'SaveData'); 



                            This is the bottom portion of the script, hopefully enough to give you an idea. Would it be better to change the name of Cell each time it is saved in the .mat file and -append it? Perhaps a loop to recognize the presence of a value in the existing Data.mat and placing it a row below it? 
+0

嗨,我想,既然你對循環來說是新的,這意味着你對編程也是新手。我不是百分百滿意你想要的問題,但如果我沒有完全誤解,我認爲還有其他方法可以做到這一點。爲了能夠給出更好的答案,你能否闡述以下幾點:什麼是樂高,樂高和圖片有什麼區別?它應該是一張樂高積木的圖片,並且應該是**的句子「......運行樂高積木的圖片並......」。此外,如果您能夠上傳短代碼示例,則會更容易理解。 – patrik

+0

樂高是一個玩具積木。圖片是這個玩具塊。所有照片都保持所有變量相同。本質上,腳本找到白色背景和圖片之間的區別,並輸出變量:大小,形狀,顏色。我們把這些變量作爲一個字符串放在一個變量名爲Cell的單元中。我們基本上每次運行此腳本時都要保存每個單元格的值。 – Carpenterguy

+0

我已將腳本的底部添加爲原始帖子的編輯。是否會更好地啓動並創建一個循環來重新命名Cell變量?我們遇到的一個大問題是Cell值會覆蓋存儲在Data.mat中的值。每次更改變量名稱都會將新值添加到數據中。使用保存(和 - 附加,正確? – Carpenterguy

回答

0

一種方法是將保存不同的文件不同部分,每個循環的itertion i.e.一個文件。

喜歡的東西:

for k = 1:n 

    % Do your processing here. 
    % Say that the interesting variables are A, B and C. 

    filename = ['Data_' num2str(k, '%06i') '.mat']; 
    save(filename, 'A', 'B', 'C'); 

end 

通過這種方法在其中運行腳本的文件夾將包含n數據文件被稱爲Data_000000.matData_000001.mat,...對應n迭代。

如果你喜歡有一個mat文件,那麼通常最好在循環後只保存一次。例如:

for k = 1:n 

    % Do your processing here. 
    % Say that the interesting variables are A, B and C. 

    A(k) = ....;   
    B(k) = ....; 
    C(k) = ....; 

end 

% Save the results 
save('Data.mat', 'A', 'B', 'C'); 

最佳,

+0

這似乎有點混亂,因爲問題似乎是範圍問題。通常使用'functions'用於分隔範圍,但我認爲OP不知道函數還沒有。通常情況下,保存到文件不是建議範圍相關問題。此外,第二種解決方案可能無法工作,因爲OP聲明他們需要**「清除工作區」**。 – patrik

+0

@patrik很難知道OP的想法是什麼,我的感覺是,它畢竟不是範圍問題。你對第二種方法是正確的,儘管它取決於OP如何使用「清除」。很難用這麼少的信息來分辨。 – Ratbert

+0

我們在命令窗口中輸入clear,如果有幫助的話。 – Carpenterguy

1

你還是在這艱辛的道路去。如果你有一個你需要處理n圖片列表,添加一個循環,像這樣:

for k = 1:n 
    %// Process picture k, generating newCell 
    %// then append newCell to the end of allCells array 
    allCells(k,:) = newCell; 
end 

現在,如果你還需要將這些保存到一個文件,你可以節省剛allCells,讓您的所有數據立刻。

如果沒有圖像列表事前,你仍然可以通過加載陣列,添加新行,然後再保存它實現你的目標:

load('allCells.mat'); 
%// Process picture, generating newCell 
allCells(end+1,:) = newCell; 
save('allCells.mat',allCells);