How can I generate variable names automatically based on my data?

9 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2018 年 7 月 11 日
コメント済み: Stephen23 2018 年 7 月 30 日
I want to create multiple variables to the Workspace such as: 'x1', 'x2', 'x3', ... and so on depending on how long my data is.
How can I do this in MATLAB?

採用された回答

MathWorks Support Team
MathWorks Support Team 2018 年 7 月 11 日
You can use the "assignin" function in a for loop to do this:
>> for i = 1:5
>> assignin('base',strcat('x',num2str(i)),i);
>> end
  1 件のコメント
Stephen23
Stephen23 2018 年 7 月 30 日
The assignin help states "Assigning to variables in the caller workspace can make code more difficult to understand, give surprising results to the user (unexpected or redefined variables in their workspace), and have a negative performance impact. The best practice is to have the function return the variables as output arguments."-
The Share Data Between Workspaces page states "Like global variables, these functions carry risks of overwriting existing data. Use them sparingly.... For other programming applications, consider argument passing and the techniques described in Alternatives to the eval Function."

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by