フィルターのクリア

How do I declare multiple variables with a rule for naming?

4 ビュー (過去 30 日間)
Oyin
Oyin 2023 年 10 月 29 日
コメント済み: Stephen23 2023 年 10 月 29 日
If I want to do something like this:
A41 = B(4,1);
A42 = B(4,2);
A43 = B(4,3);
A31 = B(3,1);
A32 = B(3,2);
A33 = B(3,3);
or something like this:
B(1,1) = A11;
B(1,2) = A12;
B(1,3) = A13;
B(1,4) = A14;
Is there a one- or two-liner way of doing this pattern? Can you automate variable naming? Can you do something like passing the names of variables as inputs to functions?What is the best paradigm in MATLAB for this kind of thing altogether?
  1 件のコメント
Stephen23
Stephen23 2023 年 10 月 29 日
"Is there a one- or two-liner way of doing this pattern?"
Yes, if you want to force yourself into writing slow, complex, inefficient, obfuscated buggy code that is harder to debug.
"Can you automate variable naming?"
I could.... but I would not.
"Can you do something like passing the names of variables as inputs to functions?"
Sure... but then trying to access that variable will be slow and complex. Best avoided.
The MATLAB documentation clearly states that the recommended day to pass data to functions is as input/output arguments:
Passing input/output arguments is the most efficient and most robust approach by far. So far you have not given any reason why you cannot do this.
"What is the best paradigm in MATLAB for this kind of thing altogether?"
The best paradigm is to use matrices/arrays and indexing. It is even in the name: "MATLAB" comes from "MATrix LABoratory" and not from "lets dynamically name lots of variables in the workspace and make processing the data harder". The paradigm that you are attempting is so bad that there is an entire page of the documentation speicifically advising against it, which states "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended."
So now you have both the MATLAB documentation and also experienced MATLAB users telling you to avoid your approach:

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2023 年 10 月 29 日
Is there a one- or two-liner way of doing this pattern?
Yes, there is a way. But that does not mean it is a good idea.
Can you automate variable naming?
Yes, you can automate variable naming. But that does not mean it is a good idea.
Can you do something like passing the names of variables as inputs to functions
Only if the function has been specifically designed to expect a name, in which case the function would have to ask the calling environment to provide a value for the variable. MATLAB does not provide any nice way to say "I'm passing the name of a variable now but when the called function asks for the value, give it the value associated with the name."
What is the best paradigm in MATLAB for this kind of thing altogether?
The best paradigm is don't do that! -- use indexing.
Generating the names of variables is slow and is error prone.
In https://www.mathworks.com/matlabcentral/answers/2020976-i-want-to-store-the-vector-from-each-for-loop-iteration-how-can-i-do-this#answer_1310391 I show code that is "intended" to create variables a, b, c, d.... z, aa, ab, ac, ... az, ba, bb, .. bz, and so on . Which had to be coded to skip generating "if" as a variable name because MATLAB makes it an error to use keywords as a variable name. That code is designed to generate only 256 different variables, and with 26 possibilities at each position, (26)*(26) = 576 which is more than 256, so you would expect to be able to generate all 256 variables using variable names that are at most two characters. But if you look at the output, you will see that most of the variable names actually generated are rather long... 63 characters. Why? What went wrong?
Until you can debug and repair the code example I gave there, you should not consider generating variable names at run-time yourself.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by