【问题标题】:MATLAB: A loop to check for existing variablesMATLAB:检查现有变量的循环
【发布时间】:2015-04-07 04:06:45
【问题描述】:

我正在编写一个运行图片或乐高积木并输出其大小、形状和颜色的脚本。我们编写脚本的方式,每次使用不同乐高的新图片时,都必须清除变量的工作区。我们必须对我们通过脚本运行的每个乐高进行分类。我们将变量保存到 .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?

【问题讨论】:

  • 嗨,我想既然您是循环新手,那意味着您也是编程新手。我不是 100% 知道你想要什么,但如果我没有完全误解,我认为还有其他方法可以做到这一点。为了能够给出更好的答案,您能否解释以下内容:什么是乐高积木,乐高积木和图片有什么区别?它应该是乐高积木的图片吗?句子应该是“...运行一张乐高积木的图片和...”。此外,如果您能够上传简短的代码示例,将会更容易理解。
  • 乐高积木是玩具积木。图片是这个玩具积木。所有照片都是在保持所有变量相同的情况下拍摄的。本质上,脚本会找到白色背景和图片之间的差异并输出变量:大小、形状、颜色。我们将这些变量作为字符串放在变量名为 Cell 的单个单元格中。我们实际上是在每次运行此脚本时都尝试保存每个 Cell 值。
  • 我已将脚本的底部添加为对原始帖子的编辑。 -append 并创建一个循环来每次重命名 Cell 变量会更好吗?我们遇到的一个大问题是单元格值覆盖了存储在 Data.mat 中的值。每次更改变量名称都会使用 Save( 和 -append 将新值添加到 Data.mat,对吗?
  • 既然你有很多复制粘贴的代码,我会说你会在使用函数时受益。这也可以用来限制范围(这样您就不必在完成之前清除工作空间)。大量复制粘贴的代码通常意味着可以做得更好。此外,我真的不明白你为什么一直保存到文件和加载。您想将不同的变量SaveData 保存到多个不同的文件中吗?如果不是,我建议您使用结构数组。然后每个元素可以有两个字段。 name(或者可能是 id)和 data。此外,还有元胞数组。
  • 此外,我在代码中没有看到clear,因此需要更好地解释该部分以适应问题。

标签: matlab loops


【解决方案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);

【讨论】:

    【解决方案2】:

    一种方法是为不同的片段保存不同的文件,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
    

    使用这种方法,运行脚本的文件夹将包含名为 Data_000000.matData_000001.mat、... 的 n 数据文件,对应于 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');
    

    最好的,

    【讨论】:

    • 这似乎有点乱,因为问题似乎是范围问题。通常functions 用于分隔范围,但我认为OP 还不知道功能。对于范围相关的问题,通常不建议保存到文件。此外,第二个解决方案可能不起作用,因为 OP 声明他们需要“清除工作区”
    • @patrik 很难知道 OP 的想法是什么,我的感觉是这毕竟不是范围问题。第二种方法是正确的,尽管它取决于 OP 如何使用clear。这么少的信息很难说。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    相关资源
    最近更新 更多