MATLAB:如何在另一个变量名称中使用变量值?
我想知道这是否可能。我的代码如下所示:
indexStop = find(firstinSeq(x,4) ~= ...
littledataPassed(y:length(littledataPassed),4), 1, 'first');
for z= 0:(n-1)
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == z), 1, 'first');
if isempty(indexProcess)
msgLength[n](countmsgLength[n],:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else
msgLength[n](countmsgLength[n],:)= [firstinSeq(x,:) ...
littledataPassed(y+indexProcess-1,:)];
end
countmsgLength[n]= countmsgLength[n] + 1;
end
我希望将所有读取 [n]
的位置切换为 n
的实际值,这样我就可以使用它来将数据添加到九个不同的变量,格式为 msgLength#
。我尝试过搜索教程以及其他内容,但没有看到有关该主题的任何内容。
I am wondering if this is possible. My code looks like this:
indexStop = find(firstinSeq(x,4) ~= ...
littledataPassed(y:length(littledataPassed),4), 1, 'first');
for z= 0:(n-1)
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == z), 1, 'first');
if isempty(indexProcess)
msgLength[n](countmsgLength[n],:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else
msgLength[n](countmsgLength[n],:)= [firstinSeq(x,:) ...
littledataPassed(y+indexProcess-1,:)];
end
countmsgLength[n]= countmsgLength[n] + 1;
end
I am hoping to have everywhere that reads [n]
be switched to the actual value of n
, such that I could use this to add data to nine different variables in the format msgLength#
. I have tried searching tutorials and what not but haven't seen anything on the topic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用数组或元胞数组几乎总是更好a> 来存储数据,而不是创建一堆名为
a1
、a2
、a3
等的变量。例如,您可以初始化msgLength
作为包含n
元素的元胞数组:您可以使用大括号访问
msgLength
的元胞:您的变量
countmsgLength
可以只是一个常规的数字数组,因为它似乎只存储n
值。您只需将方括号更改为括号(即[n]
更改为(n)
)。但是,如果您确实想创建
n
个单独的变量,您可能最终会使用EVAL 函数。 这个问题和这个问题展示了如何使用以下值创建变量名称的一些示例另一个变量。It's almost always better to use arrays or cell arrays to store data than to create a bunch of variables named
a1
,a2
,a3
, etc. For example, you can initializemsgLength
as a cell array withn
elements:And you can access cells of
msgLength
using curly braces:Your variable
countmsgLength
can just be a regular numeric array, since it appears to only storen
values. You would just have to change the square brackets to parentheses (i.e.[n]
to(n)
).However, if you really want to create
n
separate variables, you will likely end up using the EVAL function. This question and this question show some examples of how to create variables names using the value of another variable.如果您绝对必须使用多个变量,请结构 动态字段名使用 () 表示法会很方便。如果您输入以下内容:
您将获得一个包含两个字段的结构:
尽管如此,我还是同意 gnovice 的建议,即使用数组而不是多个变量来存储数据。您可以简单地向
msgLength
添加另一个维度,因此第 7 行变为:If you absolutely have to use multiple variables, structure dynamic fieldnames using () notation would be handy. If you enter the following:
You'll get a structure with two fields:
Despite this, I second gnovice's suggestion of using an array to store data instead of several variables. You could simply add another dimension to
msgLength
so line 7 becomes: