Opening different files in for loop with dlmread?

3 ビュー (過去 30 日間)
Emanuele Joy
Emanuele Joy 2018 年 5 月 22 日
コメント済み: Emanuele Joy 2018 年 5 月 22 日
I'm trying to open .txt files caesar1, caesar2, and caesar3 in a for loop.
for k = 1:3
N = string([1,2,3]);
caesarN = strcat('caesar',N);
caesarT = strcat(caesarN, '.txt');
caesar(k) = dlmread(caesarT);
end
I do end up getting a string array caesarT = [caesar1.txt, caesar2.txt, and caesar3.txt] but dlmread won't open them for some reason (says that Filename must be a character vector). I tried to char(caesarT), but it still doesn't work. What am I missing here? Do I just need to somehow add the characters ' ' to my caesar# strings? (e.g. literally have the string be 'caesar1.txt'... instead of caesar.txt)

採用された回答

jonas
jonas 2018 年 5 月 22 日
編集済み: jonas 2018 年 5 月 22 日
You need to call one file at a time in dlmread. Change to:
for k = 1:3
N = string([1,2,3]);
caesarN = strcat('caesar',N);
caesarT = strcat(caesarN, '.txt');
caesar(k) = dlmread(caesarT{k});
end
You can also use something like this, to generate the names
fnames = dir('filepath\ceasar*');
all files in the current dir beginning with 'ceasar' are stored in fnames.name
  1 件のコメント
Emanuele Joy
Emanuele Joy 2018 年 5 月 22 日
Almost, I set it as caesar{k} = dlmread(caesarT(k)) and it worked. Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by