MATLAB:如何在另一个变量名称中使用变量值?

发布于 2024-09-11 07:01:29 字数 817 浏览 5 评论 0原文

我想知道这是否可能。我的代码如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心舞飞扬 2024-09-18 07:01:29

使用数组或元胞数组几乎总是更好a> 来存储数据,而不是创建一堆名为 a1a2a3 等的变量。例如,您可以初始化 msgLength 作为包含 n 元素的元胞数组:

msgLength = cell(1,n);

您可以使用大括号访问 msgLength 的元胞:

msgLength{n} = ...  %# Assign something to cell n

您的变量 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 initialize msgLength as a cell array with n elements:

msgLength = cell(1,n);

And you can access cells of msgLength using curly braces:

msgLength{n} = ...  %# Assign something to cell n

Your variable countmsgLength can just be a regular numeric array, since it appears to only store n 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.

乙白 2024-09-18 07:01:29

如果您绝对必须使用多个变量,请结构 动态字段名使用 () 表示法会很方便。如果您输入以下内容:

>> n = 1;
>> data.(sprintf('msgLength%u', n)) = [1 2 3 4];
>> n = 2;
>> data.(sprintf('msgLength%u', n)) = [5 6 7 8];

您将获得一个包含两个字段的结构:

>> data
data = 
   msgLength1: [1 2 3 4]
   msgLength2: [5 6 7 8]

尽管如此,我还是同意 gnovice 的建议,即使用数组而不是多个变量来存储数据。您可以简单地向 msgLength 添加另一个维度,因此第 7 行变为:

msgLength(n,countmsgLength(n),:)= [firstinSeq(x,:) [0 0 0 0 0 0]];

If you absolutely have to use multiple variables, structure dynamic fieldnames using () notation would be handy. If you enter the following:

>> n = 1;
>> data.(sprintf('msgLength%u', n)) = [1 2 3 4];
>> n = 2;
>> data.(sprintf('msgLength%u', n)) = [5 6 7 8];

You'll get a structure with two fields:

>> data
data = 
   msgLength1: [1 2 3 4]
   msgLength2: [5 6 7 8]

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:

msgLength(n,countmsgLength(n),:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文