MATLAB:如何在另一个变量名称中使用变量值? [英] MATLAB: How can I use a variables value in another variables name?

查看:1582
本文介绍了MATLAB:如何在另一个变量名称中使用变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否可能.我的代码如下:

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

我希望将读取[n]的所有位置都切换为n的实际值,这样我就可以使用它以msgLength#格式将数据添加到9个不同的变量中.我尝试搜索教程,但没有找到关于该主题的任何内容.

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.

推荐答案

使用数组或

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);

您可以使用花括号访问msgLength的单元格:

And you can access cells of msgLength using curly braces:

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

您的变量countmsgLength只能是常规数字数组,因为它似乎仅存储n值.您只需将方括号更改为括号(即[n](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)).


但是,如果您真的想要创建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.

这篇关于MATLAB:如何在另一个变量名称中使用变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆