【发布时间】: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,因此需要更好地解释该部分以适应问题。