在Matlab中命名大量变量

时间:2015-09-17 13:36:55

标签: matlab

我想在Mat实验室中命名几个变量,如下所示:

变量1 变量2 variable3 ... variable100

我不能使用1x100矩阵,因为它们都是矩阵。

命名变量的解决方案是什么,只是在数字部分中有所不同,因为我得到了顶部的示例。

1 个答案:

答案 0 :(得分:4)

不,你不想像这样命名你的变量。你有很多选择,这些选择要好得多。

采取一些示例数据:

# Below we ADD ON to the existing SqlObject with a list-type member
# and populate the list with proper pointers to each register/register-
# set. This is PFM!

id_name_re = "\W*id\D*(\d+)"

# SqlObject has a feature to block adding attributes.... This will override
self.regs._attr_lock = None

# add a list-type placeholder in the SqlObj for a id array
setattr(self.regs, 'id', [])

# now get a list of all SqlObject  members called id<#> like:
#  ['id13', 'id12', 'id11', 'id10', 'id9', 'id8', 'id14 ...
id_list = [id_member  for id_member in self.regs.__dict__ if re.match(id_name_re, id_member)]

# Sort the list since we need to place them in the new id[] list
# sequentially
id_list = sorted(id_list, key=h.natural_sort_key)

# now go through the list and create a new list element and populate
# it with the SqlObject  goofy-name which is not in a list-type format
for id_member in id_list:
    offset = int(re.match(id_name_re, id_member).group(1))

    # this is NEEDED!. It causes the SqlObject  to rescan and keep
    # everything updated ('_' is kinda like /dev/null)
    _ = eval("self.regs.id%d" % (offset))

    self.regs.id.append(self.regs.__dict__[id_member].__getitem__.__self__)

每个都是A = rand(200); B = rand(200); C = rand(200); 矩阵。

MATLAB支持multidimensional arrays

200x200

您可以将数据嵌套在cell array

mydata_matrix(:, :, 1) = A;
mydata_matrix(:, :, 2) = B;
mydata_matrix(:, :, 3) = C;

或者您可以使用structure

mydata_cell{1} = A;
mydata_cell{2} = B;
mydata_cell{3} = C;

所有这些都很容易迭代,并且不会污染您的工作区。