MATLAB:检查现有变量的循环

时间:2015-04-07 04:06:45

标签: matlab loops

我正在编写一个运行图片或乐高的脚本,并输出其大小,形状和颜色。我们编写脚本的方式,每次我们使用不同乐高的新图片时,我们必须清除变量的工作空间。我们必须对通过脚本运行的每个lego进行编目。我们将变量转到.mat文件,但是每次运行脚本时似乎不能一次保存多个变量,保存的变量将替换为新值。我已经确定了解决这个问题的最佳方法是通过循环运行.mat来检查现有值,如果存在值,则将值保存在现有值下面的行中。任何帮助都会非常感激,因为我是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?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

一种方法是为每个循环保存不同文件的不同文件,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

使用此方法,运行脚本的文件夹将包含名为nData_000000.mat的{​​{1}}个数据文件,...对应于Data_000001.mat次迭代。

如果您希望有一个mat文件,那么通常最好在循环后只保存一次。例如:

n

最佳,