我正在編寫一個腳本,用於運行圖片或樂高並輸出其大小,形狀和顏色。我們編寫腳本的方式,每當我們使用不同樂高的新圖片時,我們都必須清除變量的工作空間。我們必須對我們通過腳本運行的每個樂高進行分類。我們將變量發送到.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?
嗨,我想,既然你對循環來說是新的,這意味着你對編程也是新手。我不是百分百滿意你想要的問題,但如果我沒有完全誤解,我認爲還有其他方法可以做到這一點。爲了能夠給出更好的答案,你能否闡述以下幾點:什麼是樂高,樂高和圖片有什麼區別?它應該是一張樂高積木的圖片,並且應該是**的句子「......運行樂高積木的圖片並......」。此外,如果您能夠上傳短代碼示例,則會更容易理解。 – patrik
樂高是一個玩具積木。圖片是這個玩具塊。所有照片都保持所有變量相同。本質上,腳本找到白色背景和圖片之間的區別,並輸出變量:大小,形狀,顏色。我們把這些變量作爲一個字符串放在一個變量名爲Cell的單元中。我們基本上每次運行此腳本時都要保存每個單元格的值。 – Carpenterguy
我已將腳本的底部添加爲原始帖子的編輯。是否會更好地啓動並創建一個循環來重新命名Cell變量?我們遇到的一個大問題是Cell值會覆蓋存儲在Data.mat中的值。每次更改變量名稱都會將新值添加到數據中。使用保存(和 - 附加,正確? – Carpenterguy