ループ内でテーブルを番号つきで管理する方法

7 views (last 30 days)
Kog
Kog on 14 Jun 2019
Commented: Kog on 17 Jun 2019
フォルダ内のdatファイルをすべて読み込み、fie1,fie2...というtableで管理したいのですが
知恵をいただけないでしょうか。
datFileList = dir(['*.dat']);
datNum = size(datFileList);
kk = 1;
for kk = 1 : datNum(1)
disp(kk)
datFileName = char(datFileList(kk).name); % char型に変換
HeaderlinesIn=6;
tmp = importdata(datFileName,'\t',HeaderlinesIn); %headerと数値を分けてimport
headname=strrep(tmp.colheaders,'%','percent'); %headerの名前の調整、%は変数名として使えないので
vcell = genvarname(headname); %変数名がかぶらないように処理
fie{kk}= array2table(tmp.data,'VariableNames',vcell); %変数と数値を合わせてtable化
end

Accepted Answer

Kazuya
Kazuya on 15 Jun 2019
dat ファイル1つ1つの読み込み結果を、 fie1, fie2, ... という table 変数にするということですね?それであれば eval 関数でできそうですが、下記ページのよるとあまりお勧めでは無いようですのでご注意を。。
fie{kk}= array2table(tmp.data,'VariableNames',vcell); %変数と数値を合わせてtable化
のところを
tmp2 = array2table(tmp.data,'VariableNames',vcell); %変数と数値を合わせてtable化
eval(['fie', int2str(kk),' = tmp2;']);
に変更すればOKなはず。
  1 Comment
Kog
Kog on 17 Jun 2019
お返事遅くなり申し訳ありません。
eval関数ですが、ちらっと当該ページのヘルプを読んだのですが、何が悪いのかわからなかったです…。
当面自分のほうこちらで問題ありませんので、使われていただくと思います!
ありがとうございました。

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!