MATLAB: How can I use a variables value in another variables name?

后端 未结 2 2399
有刺的猬
有刺的猬 2020-12-06 22:46

I am wondering if this is possible. My code looks like this:

indexStop = find(firstinSeq(x,4) ~= ...
                 littledataPassed(y:length(littledataPas         


        
2条回答
  •  一整个雨季
    2020-12-06 23:34

    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.

提交回复
热议问题