data_n=tableデータを任意の数作りたいのですが、以下ではエラーがでてしまします。
良い方法はありますでしょうか 。
for i= 1:id_length
eval('data%i=%s;',i,rawdata(:,"Load_Name"));
end

2 件のコメント

Dyuman Joshi
Dyuman Joshi 2024 年 2 月 20 日
編集済み: Dyuman Joshi 2024 年 2 月 20 日
DO NOT USE EVAL.
Dynamically naming variables (and subsequently using eval) is not a good coding practice. That is explained in length here - https://in.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
This (official) documentation page also advices to avoid using eval for the same - https://in.mathworks.com/help/matlab/matlab_prog/string-evaluation.html
A robust alternative (for reading data into MATLAB) is to read the data using the appropriate function, e.g - load for .mat files, and readtable/readmatrix/readcell for various different file formats.
If you want to access data from an existing variable in MATLAB, use indexing.
晃平
晃平 2024 年 2 月 21 日
Thank you for your comment.
It was very helpful.

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

 採用された回答

Tak
Tak 2024 年 2 月 20 日
編集済み: Tak 2024 年 2 月 20 日

0 投票

eval は書式演算子を受け付けません。
以下のように文字列を連結するか、書式演算子を使うなら sprintf などで実行可能なMATLAB式を作成する必要があります。
文字列の連結の場合
eval(['data' num2str(i) ' = rawdata(:,"Load_Name");'])
書式演算子を使うなら sprintf
eval(sprintf('data%d = rawdata(:,"Load_Name");', i))
ただし、コメントでも引用されていますように、eval で連番の変数名を生成するようなコードは好ましくないとされています。
繰り返し参照している rawdata がすべて同じ行数であれば配列やテーブル、そうでなければセル配列として管理する方が良いでしょう。
以下はセル配列に各データを代入する例です。
data = cell(1,id_length);
for i= 1:id_length
data{i} = rawdata(:,"Load_Name");
end
セル配列に格納された各データは data{n} で参照することができます。
詳細は以下のドキュメントを参照してください。

4 件のコメント

晃平
晃平 2024 年 2 月 21 日
コメントありがとうございます。
eval は奨励されて居りませんので、
以下の方法で変数をワークスペースに登録したいです。
よろしくお願いいたします。
data{1,1}:文字列変数 A,B,Cなど
data{1,2}:数値   1,2,3など
で、ワークスペースに
A=1
B=2
C=3
と登録したいです。
Dyuman Joshi
Dyuman Joshi 2024 年 2 月 21 日
編集済み: Dyuman Joshi 2024 年 2 月 21 日
@晃平, As I said above, Dynamically naming variables i.e. forcing meta-data (here the meta-data is alphabets) into variable names is not a good practice.
Please specify -
Why do you want to do that?
What would do you with that variables?
What is the objective?
Tak
Tak 2024 年 2 月 21 日
質問の内容が変わっていますが、別の質問として再投稿されましたのでそちらに回答しました。
晃平
晃平 2024 年 2 月 26 日
ご回答ありがとうございます。

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2022b

タグ

質問済み:

2024 年 2 月 20 日

コメント済み:

2024 年 2 月 26 日

Community Treasure Hunt

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

Start Hunting!